blob: 91eea8562d850fbffe28828f49d9ce4904f0f386 [file] [log] [blame]
Damien Millerd0fdd682010-12-01 12:02:14 +11001/* $OpenBSD: clientloop.c,v 1.226 2010/11/23 23:57:24 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
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100133/* Force TTY allocation */
134extern int force_tty_flag;
135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * Flag to indicate that we have received a window change signal which has
138 * not yet been processed. This will cause a message indicating the new
139 * window size to be sent to the server a little later. This is volatile
140 * because this is updated in a signal handler.
141 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000142static volatile sig_atomic_t received_window_change_signal = 0;
143static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller788f2122005-11-05 15:14:59 +1100145/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146static int in_non_blocking_mode = 0;
147
Damien Millere11e1ea2010-08-03 16:04:46 +1000148/* Time when backgrounded control master using ControlPersist should exit */
149static time_t control_persist_exit_time = 0;
150
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100152volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000153static int escape_char1; /* Escape character. (proto1 only) */
154static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100155static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000156static int exit_status; /* Used to store the command exit status. */
157static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100158static Buffer stdin_buffer; /* Buffer for stdin data. */
159static Buffer stdout_buffer; /* Buffer for stdout data. */
160static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000161static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100162static int connection_in; /* Connection to server (input). */
163static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000164static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000165static int session_closed; /* In SSH2: login session closed. */
166static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000169int session_ident = -1;
170
Darren Tuckere32cf432010-01-08 16:51:40 +1100171int session_resumed = 0;
172
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000173/* Track escape per proto2 channel */
174struct escape_filter_ctx {
175 int escape_pending;
176 int escape_char;
177};
178
179/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000180struct channel_reply_ctx {
181 const char *request_type;
182 int id, do_close;
183};
184
Darren Tucker9f407c42008-06-13 04:50:27 +1000185/* Global request success/failure callbacks */
186struct global_confirm {
187 TAILQ_ENTRY(global_confirm) entry;
188 global_confirm_cb *cb;
189 void *ctx;
190 int ref_count;
191};
192TAILQ_HEAD(global_confirms, global_confirm);
193static struct global_confirms global_confirms =
194 TAILQ_HEAD_INITIALIZER(global_confirms);
195
Ben Lindstromf28f6342001-04-04 02:03:04 +0000196/*XXX*/
197extern Kex *xxx_kex;
198
Damien Miller0e220db2004-06-15 10:34:08 +1000199void ssh_process_session2_setup(int, int, int, Buffer *);
200
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201/* Restores stdin to blocking mode. */
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000204leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205{
Damien Miller95def091999-11-25 00:26:21 +1100206 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000207 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100208 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100209 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210}
211
Damien Miller95def091999-11-25 00:26:21 +1100212/* Puts stdin terminal in non-blocking mode. */
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000215enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216{
Damien Miller95def091999-11-25 00:26:21 +1100217 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000218 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219}
220
Damien Miller5428f641999-11-25 11:54:57 +1100221/*
222 * Signal handler for the window change signal (SIGWINCH). This just sets a
223 * flag indicating that the window has changed.
224 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100225/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static void
Damien Miller95def091999-11-25 00:26:21 +1100227window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228{
Damien Miller95def091999-11-25 00:26:21 +1100229 received_window_change_signal = 1;
230 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
Damien Miller5428f641999-11-25 11:54:57 +1100233/*
234 * Signal handler for signals that cause the program to terminate. These
235 * signals must be trapped to restore terminal modes.
236 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100237/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000238static void
Damien Miller95def091999-11-25 00:26:21 +1100239signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000241 received_signal = sig;
242 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
Damien Miller5428f641999-11-25 11:54:57 +1100245/*
246 * Returns current time in seconds from Jan 1, 1970 with the maximum
247 * available resolution.
248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000251get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Damien Miller95def091999-11-25 00:26:21 +1100253 struct timeval tv;
254 gettimeofday(&tv, NULL);
255 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256}
257
Damien Millere11e1ea2010-08-03 16:04:46 +1000258/*
259 * Sets control_persist_exit_time to the absolute time when the
260 * backgrounded control master should exit due to expiry of the
261 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
262 * control master process, or if there is no ControlPersist timeout.
263 */
264static void
265set_control_persist_exit_time(void)
266{
267 if (muxserver_sock == -1 || !options.control_persist
268 || options.control_persist_timeout == 0)
269 /* not using a ControlPersist timeout */
270 control_persist_exit_time = 0;
271 else if (channel_still_open()) {
272 /* some client connections are still open */
273 if (control_persist_exit_time > 0)
274 debug2("%s: cancel scheduled exit", __func__);
275 control_persist_exit_time = 0;
276 } else if (control_persist_exit_time <= 0) {
277 /* a client connection has recently closed */
278 control_persist_exit_time = time(NULL) +
279 (time_t)options.control_persist_timeout;
280 debug2("%s: schedule exit in %d seconds", __func__,
281 options.control_persist_timeout);
282 }
283 /* else we are already counting down to the timeout */
284}
285
Damien Miller17e7ed02005-06-17 12:54:33 +1000286#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
287void
288client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000289 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000290{
291 char cmd[1024];
292 char line[512];
293 char xdisplay[512];
294 static char proto[512], data[512];
295 FILE *f;
296 int got_data = 0, generated = 0, do_unlink = 0, i;
297 char *xauthdir, *xauthfile;
298 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000299 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000300
301 xauthdir = xauthfile = NULL;
302 *_proto = proto;
303 *_data = data;
304 proto[0] = data[0] = '\0';
305
306 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
307 debug("No xauth program.");
308 } else {
309 if (display == NULL) {
310 debug("x11_get_proto: DISPLAY not set");
311 return;
312 }
313 /*
314 * Handle FamilyLocal case where $DISPLAY does
315 * not match an authorization entry. For this we
316 * just try "xauth list unix:displaynum.screennum".
317 * XXX: "localhost" match to determine FamilyLocal
318 * is not perfect.
319 */
320 if (strncmp(display, "localhost:", 10) == 0) {
321 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
322 display + 10);
323 display = xdisplay;
324 }
325 if (trusted == 0) {
326 xauthdir = xmalloc(MAXPATHLEN);
327 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100328 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000329 if (mkdtemp(xauthdir) != NULL) {
330 do_unlink = 1;
331 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
332 xauthdir);
333 snprintf(cmd, sizeof(cmd),
334 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000335 " untrusted timeout %u 2>" _PATH_DEVNULL,
336 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000337 debug2("x11_get_proto: %s", cmd);
338 if (system(cmd) == 0)
339 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000340 if (x11_refuse_time == 0) {
341 now = time(NULL) + 1;
342 if (UINT_MAX - timeout < now)
343 x11_refuse_time = UINT_MAX;
344 else
345 x11_refuse_time = now + timeout;
346 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000347 }
348 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000349
350 /*
351 * When in untrusted mode, we read the cookie only if it was
352 * successfully generated as an untrusted one in the step
353 * above.
354 */
355 if (trusted || generated) {
356 snprintf(cmd, sizeof(cmd),
357 "%s %s%s list %s 2>" _PATH_DEVNULL,
358 xauth_path,
359 generated ? "-f " : "" ,
360 generated ? xauthfile : "",
361 display);
362 debug2("x11_get_proto: %s", cmd);
363 f = popen(cmd, "r");
364 if (f && fgets(line, sizeof(line), f) &&
365 sscanf(line, "%*s %511s %511s", proto, data) == 2)
366 got_data = 1;
367 if (f)
368 pclose(f);
369 } else
370 error("Warning: untrusted X11 forwarding setup failed: "
371 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000372 }
373
374 if (do_unlink) {
375 unlink(xauthfile);
376 rmdir(xauthdir);
377 }
378 if (xauthdir)
379 xfree(xauthdir);
380 if (xauthfile)
381 xfree(xauthfile);
382
383 /*
384 * If we didn't get authentication data, just make up some
385 * data. The forwarding code will check the validity of the
386 * response anyway, and substitute this data. The X11
387 * server, however, will ignore this fake data and use
388 * whatever authentication mechanisms it was using otherwise
389 * for the local connection.
390 */
391 if (!got_data) {
392 u_int32_t rnd = 0;
393
394 logit("Warning: No xauth data; "
395 "using fake authentication data for X11 forwarding.");
396 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
397 for (i = 0; i < 16; i++) {
398 if (i % 4 == 0)
399 rnd = arc4random();
400 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
401 rnd & 0xff);
402 rnd >>= 8;
403 }
404 }
405}
406
Damien Miller5428f641999-11-25 11:54:57 +1100407/*
408 * This is called when the interactive is entered. This checks if there is
409 * an EOF coming on stdin. We must check this explicitly, as select() does
410 * not appear to wake up when redirecting from /dev/null.
411 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000414client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415{
Damien Miller95def091999-11-25 00:26:21 +1100416 int len;
417 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller5428f641999-11-25 11:54:57 +1100419 /*
420 * If standard input is to be "redirected from /dev/null", we simply
421 * mark that we have seen an EOF and send an EOF message to the
422 * server. Otherwise, we try to read a single character; it appears
423 * that for some files, such /dev/null, select() never wakes up for
424 * read for this descriptor, which means that we never get EOF. This
425 * way we will get the EOF if stdin comes from /dev/null or similar.
426 */
Damien Miller95def091999-11-25 00:26:21 +1100427 if (stdin_null_flag) {
428 /* Fake EOF on stdin. */
429 debug("Sending eof.");
430 stdin_eof = 1;
431 packet_start(SSH_CMSG_EOF);
432 packet_send();
433 } else {
Damien Miller95def091999-11-25 00:26:21 +1100434 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller95def091999-11-25 00:26:21 +1100436 /* Check for immediate EOF on stdin. */
437 len = read(fileno(stdin), buf, 1);
438 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000439 /*
440 * EOF. Record that we have seen it and send
441 * EOF to server.
442 */
Damien Miller95def091999-11-25 00:26:21 +1100443 debug("Sending eof.");
444 stdin_eof = 1;
445 packet_start(SSH_CMSG_EOF);
446 packet_send();
447 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100448 /*
449 * Got data. We must store the data in the buffer,
450 * and also process it as an escape character if
451 * appropriate.
452 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000453 if ((u_char) buf[0] == escape_char1)
454 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000455 else
Damien Miller95def091999-11-25 00:26:21 +1100456 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100457 }
Damien Miller95def091999-11-25 00:26:21 +1100458 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460}
461
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
Damien Miller5428f641999-11-25 11:54:57 +1100463/*
464 * Make packets from buffered stdin data, and buffer them for sending to the
465 * connection.
466 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Ben Lindstrombba81212001-06-25 05:01:22 +0000468static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000469client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000471 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Damien Miller95def091999-11-25 00:26:21 +1100473 /* Send buffered stdin data to the server. */
474 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100475 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100476 len = buffer_len(&stdin_buffer);
477 /* Keep the packets at reasonable size. */
478 if (len > packet_get_maxsize())
479 len = packet_get_maxsize();
480 packet_start(SSH_CMSG_STDIN_DATA);
481 packet_put_string(buffer_ptr(&stdin_buffer), len);
482 packet_send();
483 buffer_consume(&stdin_buffer, len);
484 /* If we have a pending EOF, send it now. */
485 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
486 packet_start(SSH_CMSG_EOF);
487 packet_send();
488 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
491
Damien Miller5428f641999-11-25 11:54:57 +1100492/*
493 * Checks if the client window has changed, and sends a packet about it to
494 * the server if so. The actual change is detected elsewhere (by a software
495 * interrupt on Unix); this just checks the flag and sends a message if
496 * appropriate.
497 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Ben Lindstrombba81212001-06-25 05:01:22 +0000499static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000500client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501{
Damien Miller1383bd82000-04-06 12:32:37 +1000502 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503
Damien Miller1383bd82000-04-06 12:32:37 +1000504 if (! received_window_change_signal)
505 return;
506 /** XXX race */
507 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508
Damien Millerd3444942000-09-30 14:20:03 +1100509 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000510
511 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000512 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000513 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000514 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
515 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000516 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100517 packet_put_int((u_int)ws.ws_row);
518 packet_put_int((u_int)ws.ws_col);
519 packet_put_int((u_int)ws.ws_xpixel);
520 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000521 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523}
524
Damien Miller509b0102003-12-17 16:33:10 +1100525static void
526client_global_request_reply(int type, u_int32_t seq, void *ctxt)
527{
Darren Tucker9f407c42008-06-13 04:50:27 +1000528 struct global_confirm *gc;
529
530 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
531 return;
532 if (gc->cb != NULL)
533 gc->cb(type, seq, gc->ctx);
534 if (--gc->ref_count <= 0) {
535 TAILQ_REMOVE(&global_confirms, gc, entry);
536 bzero(gc, sizeof(*gc));
537 xfree(gc);
538 }
539
Darren Tuckerf7288d72009-06-21 18:12:20 +1000540 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100541}
542
543static void
544server_alive_check(void)
545{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000546 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000547 logit("Timeout, server not responding.");
548 cleanup_exit(255);
549 }
Damien Miller509b0102003-12-17 16:33:10 +1100550 packet_start(SSH2_MSG_GLOBAL_REQUEST);
551 packet_put_cstring("keepalive@openssh.com");
552 packet_put_char(1); /* boolean: want reply */
553 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000554 /* Insert an empty placeholder to maintain ordering */
555 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100556}
557
Damien Miller5428f641999-11-25 11:54:57 +1100558/*
559 * Waits until the client can do something (some data becomes available on
560 * one of the file descriptors).
561 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000562static void
Damien Miller5e953212001-01-30 09:14:00 +1100563client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000564 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565{
Damien Miller509b0102003-12-17 16:33:10 +1100566 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000567 int timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100568 int ret;
569
Damien Miller5e953212001-01-30 09:14:00 +1100570 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000571 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller1383bd82000-04-06 12:32:37 +1000573 if (!compat20) {
574 /* Read from the connection, unless our buffers are full. */
575 if (buffer_len(&stdout_buffer) < buffer_high &&
576 buffer_len(&stderr_buffer) < buffer_high &&
577 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100578 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000579 /*
580 * Read from stdin, unless we have seen EOF or have very much
581 * buffered data to send to the server.
582 */
583 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100584 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000585
586 /* Select stdout/stderr if have data in buffer. */
587 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100588 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000589 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100590 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000591 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000592 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000593 if (session_closed && !channel_still_open() &&
594 !packet_have_data_to_write()) {
595 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100596 memset(*readsetp, 0, *nallocp);
597 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000598 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000599 } else {
600 FD_SET(connection_in, *readsetp);
601 }
Damien Miller1383bd82000-04-06 12:32:37 +1000602 }
603
Damien Miller95def091999-11-25 00:26:21 +1100604 /* Select server connection if have data to write to the server. */
605 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100606 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100607
Damien Miller5428f641999-11-25 11:54:57 +1100608 /*
609 * Wait for something to happen. This will suspend the process until
610 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000611 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100612 */
Damien Miller95def091999-11-25 00:26:21 +1100613
Damien Millere11e1ea2010-08-03 16:04:46 +1000614 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
615 if (options.server_alive_interval > 0 && compat20)
616 timeout_secs = options.server_alive_interval;
617 set_control_persist_exit_time();
618 if (control_persist_exit_time > 0) {
619 timeout_secs = MIN(timeout_secs,
620 control_persist_exit_time - time(NULL));
621 if (timeout_secs < 0)
622 timeout_secs = 0;
623 }
624 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100625 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000626 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000627 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100628 tv.tv_usec = 0;
629 tvp = &tv;
630 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000631
Damien Miller509b0102003-12-17 16:33:10 +1100632 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
633 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100634 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000635
636 /*
637 * We have to clear the select masks, because we return.
638 * We have to return, because the mainloop checks for the flags
639 * set by the signal handlers.
640 */
Damien Miller79faeff2001-11-12 11:06:32 +1100641 memset(*readsetp, 0, *nallocp);
642 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000643
Damien Miller95def091999-11-25 00:26:21 +1100644 if (errno == EINTR)
645 return;
646 /* Note: we might still have data in the buffers. */
647 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
648 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100649 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100650 } else if (ret == 0)
651 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000652}
653
Ben Lindstrombba81212001-06-25 05:01:22 +0000654static void
Damien Millerad833b32000-08-23 10:46:23 +1000655client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656{
Damien Miller95def091999-11-25 00:26:21 +1100657 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000658 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000659 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
660 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000661 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000662 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
663 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100665 leave_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666
Damien Miller5428f641999-11-25 11:54:57 +1100667 /*
668 * Free (and clear) the buffer to reduce the amount of data that gets
669 * written to swap.
670 */
Damien Millerad833b32000-08-23 10:46:23 +1000671 buffer_free(bin);
672 buffer_free(bout);
673 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
Damien Miller95def091999-11-25 00:26:21 +1100675 /* Send the suspend signal to the program itself. */
676 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Darren Tucker5d78de62004-11-05 20:35:44 +1100678 /* Reset window sizes in case they have changed */
679 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Damien Miller95def091999-11-25 00:26:21 +1100681 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000682 buffer_init(bin);
683 buffer_init(bout);
684 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100686 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687}
688
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100690client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000692 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000693 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller5428f641999-11-25 11:54:57 +1100695 /*
696 * Read input from the server, and add any such data to the buffer of
697 * the packet subsystem.
698 */
Damien Miller95def091999-11-25 00:26:21 +1100699 if (FD_ISSET(connection_in, readset)) {
700 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000701 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
702 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000703 /*
704 * Received EOF. The remote host has closed the
705 * connection.
706 */
707 snprintf(buf, sizeof buf,
708 "Connection to %.300s closed by remote host.\r\n",
709 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711 quit_pending = 1;
712 return;
Damien Miller95def091999-11-25 00:26:21 +1100713 }
Damien Miller5428f641999-11-25 11:54:57 +1100714 /*
715 * There is a kernel bug on Solaris that causes select to
716 * sometimes wake up even though there is no data available.
717 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000718 if (len < 0 &&
719 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100720 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Damien Miller95def091999-11-25 00:26:21 +1100722 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000723 /*
724 * An error has encountered. Perhaps there is a
725 * network problem.
726 */
727 snprintf(buf, sizeof buf,
728 "Read from remote host %.300s: %.100s\r\n",
729 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100730 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100731 quit_pending = 1;
732 return;
733 }
734 packet_process_incoming(buf, len);
735 }
Damien Miller1383bd82000-04-06 12:32:37 +1000736}
737
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000738static void
Damien Miller5771ed72008-05-19 15:35:33 +1000739client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000740{
Damien Miller5771ed72008-05-19 15:35:33 +1000741 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
742 char errmsg[256];
743 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000744
Damien Miller5771ed72008-05-19 15:35:33 +1000745 /* XXX supress on mux _client_ quietmode */
746 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100747 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000748
Damien Miller5771ed72008-05-19 15:35:33 +1000749 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
750 debug2("%s request accepted on channel %d",
751 cr->request_type, c->self);
752 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
753 if (tochan) {
754 snprintf(errmsg, sizeof(errmsg),
755 "%s request failed\r\n", cr->request_type);
756 } else {
757 snprintf(errmsg, sizeof(errmsg),
758 "%s request failed on channel %d",
759 cr->request_type, c->self);
760 }
761 /* If error occurred on primary session channel, then exit */
762 if (cr->do_close && c->self == session_ident)
763 fatal("%s", errmsg);
764 /* If error occurred on mux client, append to their stderr */
765 if (tochan)
766 buffer_append(&c->extended, errmsg, strlen(errmsg));
767 else
768 error("%s", errmsg);
769 if (cr->do_close) {
770 chan_read_failed(c);
771 chan_write_failed(c);
772 }
Damien Miller0e220db2004-06-15 10:34:08 +1000773 }
Damien Miller5771ed72008-05-19 15:35:33 +1000774 xfree(cr);
775}
Damien Miller0e220db2004-06-15 10:34:08 +1000776
Damien Miller5771ed72008-05-19 15:35:33 +1000777static void
778client_abandon_status_confirm(Channel *c, void *ctx)
779{
780 xfree(ctx);
781}
782
783static void
784client_expect_confirm(int id, const char *request, int do_close)
785{
786 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
787
788 cr->request_type = request;
789 cr->do_close = do_close;
790
791 channel_register_status_confirm(id, client_status_confirm,
792 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000793}
794
Darren Tucker9f407c42008-06-13 04:50:27 +1000795void
796client_register_global_confirm(global_confirm_cb *cb, void *ctx)
797{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000798 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000799
800 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000801 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
802 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
803 if (++last_gc->ref_count >= INT_MAX)
804 fatal("%s: last_gc->ref_count = %d",
805 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000806 return;
807 }
808
809 gc = xmalloc(sizeof(*gc));
810 gc->cb = cb;
811 gc->ctx = ctx;
812 gc->ref_count = 1;
813 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
814}
815
Damien Miller0e220db2004-06-15 10:34:08 +1000816static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000817process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000818{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000819 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100820 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000821 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100822 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100823 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100824 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000825
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100826 bzero(&fwd, sizeof(fwd));
827 fwd.listen_host = fwd.connect_host = NULL;
828
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100829 leave_raw_mode(force_tty_flag);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000830 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000831 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000832 if (s == NULL)
833 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100834 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000835 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000836 if (*s == '-')
837 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000838 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000839 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000840
Darren Tucker1973c882004-05-24 10:34:36 +1000841 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000842 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000843 logit(" -L[bind_address:]port:host:hostport "
844 "Request local forward");
845 logit(" -R[bind_address:]port:host:hostport "
846 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100847 logit(" -D[bind_address:]port "
848 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000849 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000850 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100851 if (!options.permit_local_command)
852 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000853 logit(" !args "
854 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100855 goto out;
856 }
857
858 if (*s == '!' && options.permit_local_command) {
859 s++;
860 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000861 goto out;
862 }
863
864 if (*s == 'K') {
865 delete = 1;
866 s++;
867 }
Damien Miller0164cb82008-11-05 16:30:31 +1100868 if (*s == 'L')
869 local = 1;
870 else if (*s == 'R')
871 remote = 1;
872 else if (*s == 'D')
873 dynamic = 1;
874 else {
Damien Miller996acd22003-04-09 20:59:48 +1000875 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000876 goto out;
877 }
Damien Miller0164cb82008-11-05 16:30:31 +1100878
879 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000880 logit("Not supported.");
881 goto out;
882 }
Damien Miller0164cb82008-11-05 16:30:31 +1100883 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000884 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000885 goto out;
886 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000887
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100888 while (isspace(*++s))
889 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000890
Damien Millere1537f92010-01-26 13:26:22 +1100891 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000892 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100893 cancel_port = 0;
894 cancel_host = hpdelim(&s); /* may be NULL */
895 if (s != NULL) {
896 cancel_port = a2port(s);
897 cancel_host = cleanhostname(cancel_host);
898 } else {
899 cancel_port = a2port(cancel_host);
900 cancel_host = NULL;
901 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100902 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100903 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000904 goto out;
905 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100906 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000907 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100908 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000909 logit("Bad forwarding specification.");
910 goto out;
911 }
Damien Miller0164cb82008-11-05 16:30:31 +1100912 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100913 if (channel_setup_local_fwd_listener(fwd.listen_host,
914 fwd.listen_port, fwd.connect_host,
915 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000916 logit("Port forwarding failed.");
917 goto out;
918 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100919 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000920 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100921 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000922 fwd.connect_port) < 0) {
923 logit("Port forwarding failed.");
924 goto out;
925 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100926 }
927
Darren Tuckere7066df2004-05-24 10:18:05 +1000928 logit("Forwarding port.");
929 }
930
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000931out:
932 signal(SIGINT, handler);
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100933 enter_raw_mode(force_tty_flag);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000934 if (cmd)
935 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100936 if (fwd.listen_host != NULL)
937 xfree(fwd.listen_host);
938 if (fwd.connect_host != NULL)
939 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000940}
941
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000942/*
943 * Process the characters one by one, call with c==NULL for proto1 case.
944 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000946process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
947 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000948{
949 char string[1024];
950 pid_t pid;
951 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000952 u_int i;
953 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000954 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000955 int *escape_pendingp, escape_char;
956 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000957
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000958 if (c == NULL) {
959 escape_pendingp = &escape_pending1;
960 escape_char = escape_char1;
961 } else {
962 if (c->filter_ctx == NULL)
963 return 0;
964 efc = (struct escape_filter_ctx *)c->filter_ctx;
965 escape_pendingp = &efc->escape_pending;
966 escape_char = efc->escape_char;
967 }
968
Damien Millereccb9de2005-06-17 12:59:34 +1000969 if (len <= 0)
970 return (0);
971
972 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000973 /* Get one character at a time. */
974 ch = buf[i];
975
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000976 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000977 /* We have previously seen an escape character. */
978 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000979 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000980
981 /* Process the escaped character. */
982 switch (ch) {
983 case '.':
984 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000985 snprintf(string, sizeof string, "%c.\r\n",
986 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000987 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000988
Damien Millere1537f92010-01-26 13:26:22 +1100989 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000990 chan_read_failed(c);
991 chan_write_failed(c);
992 return 0;
993 } else
994 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000995 return -1;
996
997 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000998 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100999 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001000 noescape:
1001 snprintf(string, sizeof string,
1002 "%c%c escape not available to "
1003 "multiplexed sessions\r\n",
1004 escape_char, ch);
1005 buffer_append(berr, string,
1006 strlen(string));
1007 continue;
1008 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001009 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001010 snprintf(string, sizeof string,
1011 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001012 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001013
1014 /* Restore terminal modes and suspend. */
1015 client_suspend_self(bin, bout, berr);
1016
1017 /* We have been continued. */
1018 continue;
1019
Damien Miller54c45982003-05-15 10:20:13 +10001020 case 'B':
1021 if (compat20) {
1022 snprintf(string, sizeof string,
1023 "%cB\r\n", escape_char);
1024 buffer_append(berr, string,
1025 strlen(string));
1026 channel_request_start(session_ident,
1027 "break", 0);
1028 packet_put_int(1000);
1029 packet_send();
1030 }
1031 continue;
1032
Ben Lindstromf28f6342001-04-04 02:03:04 +00001033 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001034 if (compat20) {
1035 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001036 logit("Server does not "
1037 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001038 else
1039 need_rekeying = 1;
1040 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001041 continue;
1042
Damien Millerad833b32000-08-23 10:46:23 +10001043 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001044 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001045 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001046 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001047 * Detach the program (continue to serve
1048 * connections, but put in background and no
1049 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001050 */
Damien Miller96507ef2001-11-12 10:52:25 +11001051 /* Restore tty modes. */
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001052 leave_raw_mode(force_tty_flag);
Damien Miller96507ef2001-11-12 10:52:25 +11001053
1054 /* Stop listening for new connections. */
1055 channel_stop_listening();
1056
1057 snprintf(string, sizeof string,
1058 "%c& [backgrounded]\n", escape_char);
1059 buffer_append(berr, string, strlen(string));
1060
1061 /* Fork into background. */
1062 pid = fork();
1063 if (pid < 0) {
1064 error("fork: %.100s", strerror(errno));
1065 continue;
1066 }
1067 if (pid != 0) { /* This is the parent. */
1068 /* The parent just exits. */
1069 exit(0);
1070 }
1071 /* The child continues serving connections. */
1072 if (compat20) {
1073 buffer_append(bin, "\004", 1);
1074 /* fake EOF on stdin */
1075 return -1;
1076 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001077 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001078 * Sending SSH_CMSG_EOF alone does not
1079 * always appear to be enough. So we
1080 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001081 */
1082 packet_start(SSH_CMSG_STDIN_DATA);
1083 packet_put_string("\004", 1);
1084 packet_send();
1085 /* Close stdin. */
1086 stdin_eof = 1;
1087 if (buffer_len(bin) == 0) {
1088 packet_start(SSH_CMSG_EOF);
1089 packet_send();
1090 }
1091 }
Damien Miller96507ef2001-11-12 10:52:25 +11001092 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001093
1094 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001095 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001096 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001097"%c?\r\n\
1098Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 %c. - terminate session\r\n\
1100 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001101 %cR - Request rekey (SSH protocol 2 only)\r\n\
1102 %c# - list forwarded connections\r\n\
1103 %c? - this message\r\n\
1104 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001105(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001106 escape_char, escape_char,
1107 escape_char, escape_char,
1108 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001109 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001110 } else {
1111 snprintf(string, sizeof string,
1112"%c?\r\n\
1113Supported escape sequences:\r\n\
1114 %c. - terminate connection (and any multiplexed sessions)\r\n\
1115 %cB - send a BREAK to the remote system\r\n\
1116 %cC - open a command line\r\n\
1117 %cR - Request rekey (SSH protocol 2 only)\r\n\
1118 %c^Z - suspend ssh\r\n\
1119 %c# - list forwarded connections\r\n\
1120 %c& - background ssh (when waiting for connections to terminate)\r\n\
1121 %c? - this message\r\n\
1122 %c%c - send the escape character by typing it twice\r\n\
1123(Note that escapes are only recognized immediately after newline.)\r\n",
1124 escape_char, escape_char,
1125 escape_char, escape_char,
1126 escape_char, escape_char,
1127 escape_char, escape_char,
1128 escape_char, escape_char,
1129 escape_char);
1130 }
Damien Millerad833b32000-08-23 10:46:23 +10001131 buffer_append(berr, string, strlen(string));
1132 continue;
1133
1134 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001135 snprintf(string, sizeof string, "%c#\r\n",
1136 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001137 buffer_append(berr, string, strlen(string));
1138 s = channel_open_message();
1139 buffer_append(berr, s, strlen(s));
1140 xfree(s);
1141 continue;
1142
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001143 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001144 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001145 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001146 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001147 continue;
1148
Damien Millerad833b32000-08-23 10:46:23 +10001149 default:
1150 if (ch != escape_char) {
1151 buffer_put_char(bin, escape_char);
1152 bytes++;
1153 }
1154 /* Escaped characters fall through here */
1155 break;
1156 }
1157 } else {
1158 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001159 * The previous character was not an escape char.
1160 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001161 */
1162 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001163 /*
1164 * It is. Set the flag and continue to
1165 * next character.
1166 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001167 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001168 continue;
1169 }
1170 }
1171
1172 /*
1173 * Normal character. Record whether it was a newline,
1174 * and append it to the buffer.
1175 */
1176 last_was_cr = (ch == '\r' || ch == '\n');
1177 buffer_put_char(bin, ch);
1178 bytes++;
1179 }
1180 return bytes;
1181}
1182
Ben Lindstrombba81212001-06-25 05:01:22 +00001183static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001184client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001185{
Damien Miller166fca82000-04-20 07:42:21 +10001186 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001187 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001188
Damien Miller95def091999-11-25 00:26:21 +11001189 /* Read input from stdin. */
1190 if (FD_ISSET(fileno(stdin), readset)) {
1191 /* Read as much as possible. */
1192 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001193 if (len < 0 &&
1194 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001195 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001196 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001197 /*
1198 * Received EOF or error. They are treated
1199 * similarly, except that an error message is printed
1200 * if it was an error condition.
1201 */
Damien Miller95def091999-11-25 00:26:21 +11001202 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001203 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1204 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001205 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001206 }
1207 /* Mark that we have seen EOF. */
1208 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001209 /*
1210 * Send an EOF message to the server unless there is
1211 * data in the buffer. If there is data in the
1212 * buffer, no message will be sent now. Code
1213 * elsewhere will send the EOF when the buffer
1214 * becomes empty if stdin_eof is set.
1215 */
Damien Miller95def091999-11-25 00:26:21 +11001216 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217 packet_start(SSH_CMSG_EOF);
1218 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001219 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001220 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001221 /*
1222 * Normal successful read, and no escape character.
1223 * Just append the data to buffer.
1224 */
Damien Miller95def091999-11-25 00:26:21 +11001225 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001226 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001227 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001228 * Normal, successful read. But we have an escape
1229 * character and have to process the characters one
1230 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001231 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001232 if (process_escapes(NULL, &stdin_buffer,
1233 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001234 return;
Damien Miller95def091999-11-25 00:26:21 +11001235 }
1236 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237}
1238
Ben Lindstrombba81212001-06-25 05:01:22 +00001239static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001240client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241{
Damien Miller95def091999-11-25 00:26:21 +11001242 int len;
1243 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244
Damien Miller95def091999-11-25 00:26:21 +11001245 /* Write buffered output to stdout. */
1246 if (FD_ISSET(fileno(stdout), writeset)) {
1247 /* Write as much data as possible. */
1248 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001249 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001250 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001251 if (errno == EINTR || errno == EAGAIN ||
1252 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001253 len = 0;
1254 else {
Damien Miller5428f641999-11-25 11:54:57 +11001255 /*
1256 * An error or EOF was encountered. Put an
1257 * error message to stderr buffer.
1258 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001259 snprintf(buf, sizeof buf,
1260 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001261 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001262 quit_pending = 1;
1263 return;
1264 }
1265 }
1266 /* Consume printed data from the buffer. */
1267 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268 }
Damien Miller95def091999-11-25 00:26:21 +11001269 /* Write buffered output to stderr. */
1270 if (FD_ISSET(fileno(stderr), writeset)) {
1271 /* Write as much data as possible. */
1272 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001273 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001274 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001275 if (errno == EINTR || errno == EAGAIN ||
1276 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001277 len = 0;
1278 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001279 /*
1280 * EOF or error, but can't even print
1281 * error message.
1282 */
Damien Miller95def091999-11-25 00:26:21 +11001283 quit_pending = 1;
1284 return;
1285 }
1286 }
1287 /* Consume printed characters from the buffer. */
1288 buffer_consume(&stderr_buffer, len);
1289 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290}
1291
Damien Miller5428f641999-11-25 11:54:57 +11001292/*
Damien Millerb38eff82000-04-01 11:09:21 +10001293 * Get packets from the connection input buffer, and process them as long as
1294 * there are packets available.
1295 *
1296 * Any unknown packets received during the actual
1297 * session cause the session to terminate. This is
1298 * intended to make debugging easier since no
1299 * confirmations are sent. Any compatible protocol
1300 * extensions must be negotiated during the
1301 * preparatory phase.
1302 */
1303
Ben Lindstrombba81212001-06-25 05:01:22 +00001304static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001305client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001306{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001307 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1308 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001309}
1310
Damien Millerad833b32000-08-23 10:46:23 +10001311/* scan buf[] for '~' before sending data to the peer */
1312
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001313/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1314void *
1315client_new_escape_filter_ctx(int escape_char)
1316{
1317 struct escape_filter_ctx *ret;
1318
1319 ret = xmalloc(sizeof(*ret));
1320 ret->escape_pending = 0;
1321 ret->escape_char = escape_char;
1322 return (void *)ret;
1323}
1324
Darren Tucker84c56f52008-06-13 04:55:46 +10001325/* Free the escape filter context on channel free */
1326void
1327client_filter_cleanup(int cid, void *ctx)
1328{
1329 xfree(ctx);
1330}
1331
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001332int
1333client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001334{
Damien Miller5771ed72008-05-19 15:35:33 +10001335 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1336 return 0;
1337
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001338 return process_escapes(c, &c->input, &c->output, &c->extended,
1339 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001340}
1341
Ben Lindstrombba81212001-06-25 05:01:22 +00001342static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001343client_channel_closed(int id, void *arg)
1344{
Damien Miller3ec27592001-10-12 11:35:04 +10001345 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001346 session_closed = 1;
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001347 leave_raw_mode(force_tty_flag);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001348}
1349
Damien Millerb38eff82000-04-01 11:09:21 +10001350/*
Damien Miller5428f641999-11-25 11:54:57 +11001351 * Implements the interactive session with the server. This is called after
1352 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001353 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1354 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001355 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356
Damien Miller4af51302000-04-16 11:18:38 +10001357int
Damien Millerad833b32000-08-23 10:46:23 +10001358client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359{
Damien Miller5e953212001-01-30 09:14:00 +11001360 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001361 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001362 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001363 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001364 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001365 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Damien Miller95def091999-11-25 00:26:21 +11001367 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370
Damien Miller95def091999-11-25 00:26:21 +11001371 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001372 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001373 last_was_cr = 1;
1374 exit_status = -1;
1375 stdin_eof = 0;
1376 buffer_high = 64 * 1024;
1377 connection_in = packet_get_connection_in();
1378 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001379 max_fd = MAX(connection_in, connection_out);
1380
1381 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001382 /* enable nonblocking unless tty */
1383 if (!isatty(fileno(stdin)))
1384 set_nonblock(fileno(stdin));
1385 if (!isatty(fileno(stdout)))
1386 set_nonblock(fileno(stdout));
1387 if (!isatty(fileno(stderr)))
1388 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001389 max_fd = MAX(max_fd, fileno(stdin));
1390 max_fd = MAX(max_fd, fileno(stdout));
1391 max_fd = MAX(max_fd, fileno(stderr));
1392 }
Damien Miller95def091999-11-25 00:26:21 +11001393 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001394 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395
Damien Miller95def091999-11-25 00:26:21 +11001396 /* Initialize buffers. */
1397 buffer_init(&stdin_buffer);
1398 buffer_init(&stdout_buffer);
1399 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Millerb38eff82000-04-01 11:09:21 +10001401 client_init_dispatch();
1402
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001403 /*
1404 * Set signal handlers, (e.g. to restore non-blocking mode)
1405 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1406 */
Darren Tucker07336da2004-11-05 20:02:16 +11001407 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1408 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001409 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1410 signal(SIGINT, signal_handler);
1411 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1412 signal(SIGQUIT, signal_handler);
1413 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1414 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001415 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
Damien Miller95def091999-11-25 00:26:21 +11001417 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001418 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419
Ben Lindstrom5b828322001-02-09 01:34:36 +00001420 if (compat20) {
1421 session_ident = ssh2_chan_id;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001422 if (escape_char_arg != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001423 channel_register_filter(session_ident,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001424 client_simple_escape_filter, NULL,
Darren Tucker84c56f52008-06-13 04:55:46 +10001425 client_filter_cleanup,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001426 client_new_escape_filter_ctx(escape_char_arg));
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001427 if (session_ident != -1)
1428 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001429 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001430 } else {
1431 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001432 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001433 }
Damien Millerad833b32000-08-23 10:46:23 +10001434
Damien Miller95def091999-11-25 00:26:21 +11001435 /* Main loop of the client for the interactive session mode. */
1436 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Damien Miller5428f641999-11-25 11:54:57 +11001438 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001439 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001441 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001442 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001443
1444 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001445
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001446 if (rekeying) {
1447 debug("rekeying in progress");
1448 } else {
1449 /*
1450 * Make packets of buffered stdin data, and buffer
1451 * them for sending to the server.
1452 */
1453 if (!compat20)
1454 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001456 /*
1457 * Make packets from buffered channel data, and
1458 * enqueue them for sending to the server.
1459 */
1460 if (packet_not_very_much_data_to_write())
1461 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001463 /*
1464 * Check if the window size has changed, and buffer a
1465 * message about it to the server if so.
1466 */
1467 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001469 if (quit_pending)
1470 break;
1471 }
Damien Miller5428f641999-11-25 11:54:57 +11001472 /*
1473 * Wait until we have something to do (something becomes
1474 * available on one of the descriptors).
1475 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001476 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001477 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001478 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller95def091999-11-25 00:26:21 +11001480 if (quit_pending)
1481 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001483 /* Do channel operations unless rekeying in progress. */
1484 if (!rekeying) {
1485 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001486 if (need_rekeying || packet_need_rekeying()) {
1487 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001488 xxx_kex->done = 0;
1489 kex_send_kexinit(xxx_kex);
1490 need_rekeying = 0;
1491 }
1492 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Damien Miller1383bd82000-04-06 12:32:37 +10001494 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001495 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496
Damien Miller1383bd82000-04-06 12:32:37 +10001497 if (quit_pending)
1498 break;
1499
1500 if (!compat20) {
1501 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001502 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001503 /*
1504 * Process output to stdout and stderr. Output to
1505 * the connection is processed elsewhere (above).
1506 */
Damien Miller5e953212001-01-30 09:14:00 +11001507 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001508 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509
Darren Tuckere32cf432010-01-08 16:51:40 +11001510 if (session_resumed) {
1511 connection_in = packet_get_connection_in();
1512 connection_out = packet_get_connection_out();
1513 max_fd = MAX(max_fd, connection_out);
1514 max_fd = MAX(max_fd, connection_in);
1515 session_resumed = 0;
1516 }
1517
Darren Tucker4d5cd332008-06-13 04:51:14 +10001518 /*
1519 * Send as much buffered packet data as possible to the
1520 * sender.
1521 */
Damien Miller5e953212001-01-30 09:14:00 +11001522 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001523 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001524
1525 /*
1526 * If we are a backgrounded control master, and the
1527 * timeout has expired without any active client
1528 * connections, then quit.
1529 */
1530 if (control_persist_exit_time > 0) {
1531 if (time(NULL) >= control_persist_exit_time) {
1532 debug("ControlPersist timeout expired");
1533 break;
1534 }
1535 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536 }
Damien Miller5e953212001-01-30 09:14:00 +11001537 if (readset)
1538 xfree(readset);
1539 if (writeset)
1540 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541
Damien Miller95def091999-11-25 00:26:21 +11001542 /* Terminate the session. */
1543
1544 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001545 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001546
Darren Tuckerde0c0252009-07-06 07:17:35 +10001547 if (compat20) {
1548 packet_start(SSH2_MSG_DISCONNECT);
1549 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1550 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001551 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001552 packet_send();
1553 packet_write_wait();
1554 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001555
Ben Lindstrom601e4362001-06-21 03:19:23 +00001556 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001557
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001558 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001559 leave_raw_mode(force_tty_flag);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001560
1561 /* restore blocking io */
1562 if (!isatty(fileno(stdin)))
1563 unset_nonblock(fileno(stdin));
1564 if (!isatty(fileno(stdout)))
1565 unset_nonblock(fileno(stdout));
1566 if (!isatty(fileno(stderr)))
1567 unset_nonblock(fileno(stderr));
1568
Damien Millerd6965512003-12-17 16:31:53 +11001569 /*
1570 * If there was no shell or command requested, there will be no remote
1571 * exit status to be returned. In that case, clear error code if the
1572 * connection was deliberately terminated at this end.
1573 */
1574 if (no_shell_flag && received_signal == SIGTERM) {
1575 received_signal = 0;
1576 exit_status = 0;
1577 }
1578
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001579 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001580 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001581
1582 /*
1583 * In interactive mode (with pseudo tty) display a message indicating
1584 * that the connection has been closed.
1585 */
1586 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001587 snprintf(buf, sizeof buf,
1588 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001589 buffer_append(&stderr_buffer, buf, strlen(buf));
1590 }
1591
Damien Miller95def091999-11-25 00:26:21 +11001592 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001593 while (buffer_len(&stdout_buffer) > 0) {
1594 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001595 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001596 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001597 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001598 break;
1599 }
Damien Miller95def091999-11-25 00:26:21 +11001600 buffer_consume(&stdout_buffer, len);
1601 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602
Damien Miller95def091999-11-25 00:26:21 +11001603 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001604 while (buffer_len(&stderr_buffer) > 0) {
1605 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001606 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001607 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001608 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001609 break;
1610 }
Damien Miller95def091999-11-25 00:26:21 +11001611 buffer_consume(&stderr_buffer, len);
1612 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613
Damien Miller95def091999-11-25 00:26:21 +11001614 /* Clear and free any buffers. */
1615 memset(buf, 0, sizeof(buf));
1616 buffer_free(&stdin_buffer);
1617 buffer_free(&stdout_buffer);
1618 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001619
Damien Miller95def091999-11-25 00:26:21 +11001620 /* Report bytes transferred, and transfer rates. */
1621 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001622 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1623 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1624 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1625 obytes, ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001626 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001627 verbose("Bytes per second: sent %.1f, received %.1f",
1628 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001629 /* Return the exit status of the program. */
1630 debug("Exit status %d", exit_status);
1631 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632}
Damien Millerb38eff82000-04-01 11:09:21 +10001633
1634/*********/
1635
Ben Lindstrombba81212001-06-25 05:01:22 +00001636static void
Damien Miller630d6f42002-01-22 23:17:30 +11001637client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001638{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001639 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001640 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001641 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001642 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001643 memset(data, 0, data_len);
1644 xfree(data);
1645}
Ben Lindstrombba81212001-06-25 05:01:22 +00001646static void
Damien Miller630d6f42002-01-22 23:17:30 +11001647client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001648{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001649 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001650 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001651 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001652 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001653 memset(data, 0, data_len);
1654 xfree(data);
1655}
Ben Lindstrombba81212001-06-25 05:01:22 +00001656static void
Damien Miller630d6f42002-01-22 23:17:30 +11001657client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001658{
Damien Millerb38eff82000-04-01 11:09:21 +10001659 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001660 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001661 /* Acknowledge the exit. */
1662 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1663 packet_send();
1664 /*
1665 * Must wait for packet to be sent since we are
1666 * exiting the loop.
1667 */
1668 packet_write_wait();
1669 /* Flag that we want to exit. */
1670 quit_pending = 1;
1671}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001672static void
1673client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1674{
1675 Channel *c = NULL;
1676 int remote_id, sock;
1677
1678 /* Read the remote channel number from the message. */
1679 remote_id = packet_get_int();
1680 packet_check_eom();
1681
1682 /*
1683 * Get a connection to the local authentication agent (this may again
1684 * get forwarded).
1685 */
1686 sock = ssh_get_authentication_socket();
1687
1688 /*
1689 * If we could not connect the agent, send an error message back to
1690 * the server. This should never happen unless the agent dies,
1691 * because authentication forwarding is only enabled if we have an
1692 * agent.
1693 */
1694 if (sock >= 0) {
1695 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1696 -1, 0, 0, 0, "authentication agent connection", 1);
1697 c->remote_id = remote_id;
1698 c->force_drain = 1;
1699 }
1700 if (c == NULL) {
1701 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1702 packet_put_int(remote_id);
1703 } else {
1704 /* Send a confirmation to the remote host. */
1705 debug("Forwarding authentication connection.");
1706 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1707 packet_put_int(remote_id);
1708 packet_put_int(c->self);
1709 }
1710 packet_send();
1711}
Damien Millerb38eff82000-04-01 11:09:21 +10001712
Ben Lindstrombba81212001-06-25 05:01:22 +00001713static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001714client_request_forwarded_tcpip(const char *request_type, int rchan)
1715{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001716 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001717 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001718 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001719
1720 /* Get rest of the packet */
1721 listen_address = packet_get_string(NULL);
1722 listen_port = packet_get_int();
1723 originator_address = packet_get_string(NULL);
1724 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001725 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001726
Damien Millerbd740252008-05-19 15:37:09 +10001727 debug("client_request_forwarded_tcpip: listen %s port %d, "
1728 "originator %s port %d", listen_address, listen_port,
1729 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001730
Damien Millerbd740252008-05-19 15:37:09 +10001731 c = channel_connect_by_listen_address(listen_port,
1732 "forwarded-tcpip", originator_address);
1733
Damien Miller0bc1bd82000-11-13 22:57:25 +11001734 xfree(originator_address);
1735 xfree(listen_address);
1736 return c;
1737}
1738
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001739static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740client_request_x11(const char *request_type, int rchan)
1741{
1742 Channel *c = NULL;
1743 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001744 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001745 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001746
1747 if (!options.forward_x11) {
1748 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001749 error("Warning: this is probably a break-in attempt by a "
1750 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001751 return NULL;
1752 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001753 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1754 verbose("Rejected X11 connection after ForwardX11Timeout "
1755 "expired");
1756 return NULL;
1757 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758 originator = packet_get_string(NULL);
1759 if (datafellows & SSH_BUG_X11FWD) {
1760 debug2("buggy server: x11 request w/o originator_port");
1761 originator_port = 0;
1762 } else {
1763 originator_port = packet_get_int();
1764 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001765 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001766 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001767 debug("client_request_x11: request from %s %d", originator,
1768 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001769 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001770 sock = x11_connect_display();
1771 if (sock < 0)
1772 return NULL;
1773 c = channel_new("x11",
1774 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001775 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001776 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001777 return c;
1778}
1779
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001780static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001781client_request_agent(const char *request_type, int rchan)
1782{
1783 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001784 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001785
1786 if (!options.forward_agent) {
1787 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001788 error("Warning: this is probably a break-in attempt by a "
1789 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001790 return NULL;
1791 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001792 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001793 if (sock < 0)
1794 return NULL;
1795 c = channel_new("authentication agent connection",
1796 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001797 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001798 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001799 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001800 return c;
1801}
1802
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001803int
1804client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1805{
1806 Channel *c;
1807 int fd;
1808
1809 if (tun_mode == SSH_TUNMODE_NO)
1810 return 0;
1811
1812 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001813 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001814 return -1;
1815 }
1816
1817 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1818
1819 /* Open local tunnel device */
1820 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1821 error("Tunnel device open failed.");
1822 return -1;
1823 }
1824
1825 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1826 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1827 c->datagram = 1;
1828
1829#if defined(SSH_TUN_FILTER)
1830 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1831 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001832 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001833#endif
1834
1835 packet_start(SSH2_MSG_CHANNEL_OPEN);
1836 packet_put_cstring("tun@openssh.com");
1837 packet_put_int(c->self);
1838 packet_put_int(c->local_window_max);
1839 packet_put_int(c->local_maxpacket);
1840 packet_put_int(tun_mode);
1841 packet_put_int(remote_tun);
1842 packet_send();
1843
1844 return 0;
1845}
1846
Damien Millerbd483e72000-04-30 10:00:53 +10001847/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001848static void
Damien Miller630d6f42002-01-22 23:17:30 +11001849client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001850{
1851 Channel *c = NULL;
1852 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001853 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001854 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001855
1856 ctype = packet_get_string(&len);
1857 rchan = packet_get_int();
1858 rwindow = packet_get_int();
1859 rmaxpack = packet_get_int();
1860
Damien Millere247cc42000-05-07 12:03:14 +10001861 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001862 ctype, rchan, rwindow, rmaxpack);
1863
Damien Miller0bc1bd82000-11-13 22:57:25 +11001864 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1865 c = client_request_forwarded_tcpip(ctype, rchan);
1866 } else if (strcmp(ctype, "x11") == 0) {
1867 c = client_request_x11(ctype, rchan);
1868 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1869 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001870 }
1871/* XXX duplicate : */
1872 if (c != NULL) {
1873 debug("confirm %s", ctype);
1874 c->remote_id = rchan;
1875 c->remote_window = rwindow;
1876 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001877 if (c->type != SSH_CHANNEL_CONNECTING) {
1878 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1879 packet_put_int(c->remote_id);
1880 packet_put_int(c->self);
1881 packet_put_int(c->local_window);
1882 packet_put_int(c->local_maxpacket);
1883 packet_send();
1884 }
Damien Millerbd483e72000-04-30 10:00:53 +10001885 } else {
1886 debug("failure %s", ctype);
1887 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1888 packet_put_int(rchan);
1889 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001890 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001891 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001892 packet_put_cstring("");
1893 }
Damien Millerbd483e72000-04-30 10:00:53 +10001894 packet_send();
1895 }
1896 xfree(ctype);
1897}
Ben Lindstrombba81212001-06-25 05:01:22 +00001898static void
Damien Miller630d6f42002-01-22 23:17:30 +11001899client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001900{
1901 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001902 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001903 char *rtype;
1904
1905 id = packet_get_int();
1906 rtype = packet_get_string(NULL);
1907 reply = packet_get_char();
1908
1909 debug("client_input_channel_req: channel %d rtype %s reply %d",
1910 id, rtype, reply);
1911
Damien Miller3bbd8782004-06-18 22:23:22 +10001912 if (id == -1) {
1913 error("client_input_channel_req: request for channel -1");
1914 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001915 error("client_input_channel_req: channel %d: "
1916 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001917 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1918 packet_check_eom();
1919 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001920 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001921 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001922 if (c->ctl_chan != -1) {
1923 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001924 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001925 } else if (id == session_ident) {
1926 /* Record exit value of local session */
1927 success = 1;
1928 exit_status = exitval;
1929 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001930 /* Probably for a mux channel that has already closed */
1931 debug("%s: no sink for exit-status on channel %d",
1932 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001933 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001934 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001935 }
Damien Millerd0fdd682010-12-01 12:02:14 +11001936 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001937 packet_start(success ?
1938 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001939 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001940 packet_send();
1941 }
1942 xfree(rtype);
1943}
Damien Millerc3fa4072002-01-22 23:21:58 +11001944static void
1945client_input_global_request(int type, u_int32_t seq, void *ctxt)
1946{
1947 char *rtype;
1948 int want_reply;
1949 int success = 0;
1950
1951 rtype = packet_get_string(NULL);
1952 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001953 debug("client_input_global_request: rtype %s want_reply %d",
1954 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001955 if (want_reply) {
1956 packet_start(success ?
1957 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1958 packet_send();
1959 packet_write_wait();
1960 }
1961 xfree(rtype);
1962}
Damien Millerbd483e72000-04-30 10:00:53 +10001963
Damien Miller0e220db2004-06-15 10:34:08 +10001964void
Darren Tuckerfc959702004-07-17 16:12:08 +10001965client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001966 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001967{
1968 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001969 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001970
1971 debug2("%s: id %d", __func__, id);
1972
Darren Tucker5d78de62004-11-05 20:35:44 +11001973 if ((c = channel_lookup(id)) == NULL)
1974 fatal("client_session2_setup: channel %d: unknown channel", id);
1975
Damien Miller0dac6fb2010-11-20 15:19:38 +11001976 packet_set_interactive(want_tty,
1977 options.ip_qos_interactive, options.ip_qos_bulk);
1978
Damien Miller0e220db2004-06-15 10:34:08 +10001979 if (want_tty) {
1980 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001981
1982 /* Store window size in the packet. */
1983 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1984 memset(&ws, 0, sizeof(ws));
1985
Damien Miller5771ed72008-05-19 15:35:33 +10001986 channel_request_start(id, "pty-req", 1);
Damien Miller22c97f12010-04-16 15:53:23 +10001987 client_expect_confirm(id, "PTY allocation", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001988 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001989 packet_put_int((u_int)ws.ws_col);
1990 packet_put_int((u_int)ws.ws_row);
1991 packet_put_int((u_int)ws.ws_xpixel);
1992 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001993 if (tiop == NULL)
1994 tiop = get_saved_tio();
1995 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001996 packet_send();
1997 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001998 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001999 }
2000
2001 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002002 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002003 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002004 char *name, *val;
2005
2006 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002007 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002008 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002009 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002010 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002011 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002012 continue;
2013 }
2014 *val++ = '\0';
2015
2016 matched = 0;
2017 for (j = 0; j < options.num_send_env; j++) {
2018 if (match_pattern(name, options.send_env[j])) {
2019 matched = 1;
2020 break;
2021 }
2022 }
2023 if (!matched) {
2024 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002025 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002026 continue;
2027 }
2028
2029 debug("Sending env %s = %s", name, val);
2030 channel_request_start(id, "env", 0);
2031 packet_put_cstring(name);
2032 packet_put_cstring(val);
2033 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002034 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002035 }
2036 }
2037
2038 len = buffer_len(cmd);
2039 if (len > 0) {
2040 if (len > 900)
2041 len = 900;
2042 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002043 debug("Sending subsystem: %.*s",
2044 len, (u_char*)buffer_ptr(cmd));
2045 channel_request_start(id, "subsystem", 1);
2046 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002047 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002048 debug("Sending command: %.*s",
2049 len, (u_char*)buffer_ptr(cmd));
2050 channel_request_start(id, "exec", 1);
2051 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002052 }
2053 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2054 packet_send();
2055 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002056 channel_request_start(id, "shell", 1);
2057 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002058 packet_send();
2059 }
2060}
2061
Ben Lindstrombba81212001-06-25 05:01:22 +00002062static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002063client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002064{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002065 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002066
Damien Miller1383bd82000-04-06 12:32:37 +10002067 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2068 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2069 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2070 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002071 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002072 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2073 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002074 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002075 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002076 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2077 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002078 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002079
2080 /* rekeying */
2081 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002082
2083 /* global request reply messages */
2084 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2085 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002086}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002087
Ben Lindstrombba81212001-06-25 05:01:22 +00002088static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002089client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002090{
2091 dispatch_init(NULL);
2092 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2093 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2094 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002095 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2096 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2097 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002098 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2099 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2100 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002101
2102 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002103 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002104 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2105 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002106}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002107
Ben Lindstrombba81212001-06-25 05:01:22 +00002108static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002109client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002110{
2111 client_init_dispatch_13();
2112 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2113 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2114}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002115
Ben Lindstromdb47f382001-07-04 05:10:27 +00002116static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002117client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002118{
Damien Miller1383bd82000-04-06 12:32:37 +10002119 if (compat20)
2120 client_init_dispatch_20();
2121 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002122 client_init_dispatch_13();
2123 else
2124 client_init_dispatch_15();
2125}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002126
2127/* client specific fatal cleanup */
2128void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002129cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002130{
Darren Tucker37c1b3d2010-01-09 22:26:23 +11002131 leave_raw_mode(force_tty_flag);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002132 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002133 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002134 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002135 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002136 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002137}