blob: 5608bcc2e57f637f05640aa3d96ddf4c121762b4 [file] [log] [blame]
Damien Miller1ab6a512010-06-26 10:02:24 +10001/* $OpenBSD: clientloop.c,v 1.221 2010/06/25 23:15:36 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
148/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100149volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000150static int escape_char1; /* Escape character. (proto1 only) */
151static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100152static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000153static int exit_status; /* Used to store the command exit status. */
154static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100155static Buffer stdin_buffer; /* Buffer for stdin data. */
156static Buffer stdout_buffer; /* Buffer for stdout data. */
157static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000158static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100159static int connection_in; /* Connection to server (input). */
160static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000161static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000162static int session_closed; /* In SSH2: login session closed. */
163static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000166int session_ident = -1;
167
Darren Tuckere32cf432010-01-08 16:51:40 +1100168int session_resumed = 0;
169
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000170/* Track escape per proto2 channel */
171struct escape_filter_ctx {
172 int escape_pending;
173 int escape_char;
174};
175
176/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000177struct channel_reply_ctx {
178 const char *request_type;
179 int id, do_close;
180};
181
Darren Tucker9f407c42008-06-13 04:50:27 +1000182/* Global request success/failure callbacks */
183struct global_confirm {
184 TAILQ_ENTRY(global_confirm) entry;
185 global_confirm_cb *cb;
186 void *ctx;
187 int ref_count;
188};
189TAILQ_HEAD(global_confirms, global_confirm);
190static struct global_confirms global_confirms =
191 TAILQ_HEAD_INITIALIZER(global_confirms);
192
Ben Lindstromf28f6342001-04-04 02:03:04 +0000193/*XXX*/
194extern Kex *xxx_kex;
195
Damien Miller0e220db2004-06-15 10:34:08 +1000196void ssh_process_session2_setup(int, int, int, Buffer *);
197
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198/* Restores stdin to blocking mode. */
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000201leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller95def091999-11-25 00:26:21 +1100203 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000204 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100205 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100206 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207}
208
Damien Miller95def091999-11-25 00:26:21 +1100209/* Puts stdin terminal in non-blocking mode. */
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000212enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000215 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Signal handler for the window change signal (SIGWINCH). This just sets a
220 * flag indicating that the window has changed.
221 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100222/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller95def091999-11-25 00:26:21 +1100224window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Damien Miller95def091999-11-25 00:26:21 +1100226 received_window_change_signal = 1;
227 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
231 * Signal handler for signals that cause the program to terminate. These
232 * signals must be trapped to restore terminal modes.
233 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100234/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Damien Miller95def091999-11-25 00:26:21 +1100236signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000238 received_signal = sig;
239 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Miller5428f641999-11-25 11:54:57 +1100242/*
243 * Returns current time in seconds from Jan 1, 1970 with the maximum
244 * available resolution.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000248get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 struct timeval tv;
251 gettimeofday(&tv, NULL);
252 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253}
254
Damien Miller17e7ed02005-06-17 12:54:33 +1000255#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
256void
257client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000258 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000259{
260 char cmd[1024];
261 char line[512];
262 char xdisplay[512];
263 static char proto[512], data[512];
264 FILE *f;
265 int got_data = 0, generated = 0, do_unlink = 0, i;
266 char *xauthdir, *xauthfile;
267 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000268 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000269
270 xauthdir = xauthfile = NULL;
271 *_proto = proto;
272 *_data = data;
273 proto[0] = data[0] = '\0';
274
275 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
276 debug("No xauth program.");
277 } else {
278 if (display == NULL) {
279 debug("x11_get_proto: DISPLAY not set");
280 return;
281 }
282 /*
283 * Handle FamilyLocal case where $DISPLAY does
284 * not match an authorization entry. For this we
285 * just try "xauth list unix:displaynum.screennum".
286 * XXX: "localhost" match to determine FamilyLocal
287 * is not perfect.
288 */
289 if (strncmp(display, "localhost:", 10) == 0) {
290 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
291 display + 10);
292 display = xdisplay;
293 }
294 if (trusted == 0) {
295 xauthdir = xmalloc(MAXPATHLEN);
296 xauthfile = xmalloc(MAXPATHLEN);
297 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
298 if (mkdtemp(xauthdir) != NULL) {
299 do_unlink = 1;
300 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
301 xauthdir);
302 snprintf(cmd, sizeof(cmd),
303 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000304 " untrusted timeout %u 2>" _PATH_DEVNULL,
305 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000306 debug2("x11_get_proto: %s", cmd);
307 if (system(cmd) == 0)
308 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000309 if (x11_refuse_time == 0) {
310 now = time(NULL) + 1;
311 if (UINT_MAX - timeout < now)
312 x11_refuse_time = UINT_MAX;
313 else
314 x11_refuse_time = now + timeout;
315 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000316 }
317 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000318
319 /*
320 * When in untrusted mode, we read the cookie only if it was
321 * successfully generated as an untrusted one in the step
322 * above.
323 */
324 if (trusted || generated) {
325 snprintf(cmd, sizeof(cmd),
326 "%s %s%s list %s 2>" _PATH_DEVNULL,
327 xauth_path,
328 generated ? "-f " : "" ,
329 generated ? xauthfile : "",
330 display);
331 debug2("x11_get_proto: %s", cmd);
332 f = popen(cmd, "r");
333 if (f && fgets(line, sizeof(line), f) &&
334 sscanf(line, "%*s %511s %511s", proto, data) == 2)
335 got_data = 1;
336 if (f)
337 pclose(f);
338 } else
339 error("Warning: untrusted X11 forwarding setup failed: "
340 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000341 }
342
343 if (do_unlink) {
344 unlink(xauthfile);
345 rmdir(xauthdir);
346 }
347 if (xauthdir)
348 xfree(xauthdir);
349 if (xauthfile)
350 xfree(xauthfile);
351
352 /*
353 * If we didn't get authentication data, just make up some
354 * data. The forwarding code will check the validity of the
355 * response anyway, and substitute this data. The X11
356 * server, however, will ignore this fake data and use
357 * whatever authentication mechanisms it was using otherwise
358 * for the local connection.
359 */
360 if (!got_data) {
361 u_int32_t rnd = 0;
362
363 logit("Warning: No xauth data; "
364 "using fake authentication data for X11 forwarding.");
365 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
366 for (i = 0; i < 16; i++) {
367 if (i % 4 == 0)
368 rnd = arc4random();
369 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
370 rnd & 0xff);
371 rnd >>= 8;
372 }
373 }
374}
375
Damien Miller5428f641999-11-25 11:54:57 +1100376/*
377 * This is called when the interactive is entered. This checks if there is
378 * an EOF coming on stdin. We must check this explicitly, as select() does
379 * not appear to wake up when redirecting from /dev/null.
380 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381
Ben Lindstrombba81212001-06-25 05:01:22 +0000382static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000383client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384{
Damien Miller95def091999-11-25 00:26:21 +1100385 int len;
386 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Damien Miller5428f641999-11-25 11:54:57 +1100388 /*
389 * If standard input is to be "redirected from /dev/null", we simply
390 * mark that we have seen an EOF and send an EOF message to the
391 * server. Otherwise, we try to read a single character; it appears
392 * that for some files, such /dev/null, select() never wakes up for
393 * read for this descriptor, which means that we never get EOF. This
394 * way we will get the EOF if stdin comes from /dev/null or similar.
395 */
Damien Miller95def091999-11-25 00:26:21 +1100396 if (stdin_null_flag) {
397 /* Fake EOF on stdin. */
398 debug("Sending eof.");
399 stdin_eof = 1;
400 packet_start(SSH_CMSG_EOF);
401 packet_send();
402 } else {
Damien Miller95def091999-11-25 00:26:21 +1100403 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller95def091999-11-25 00:26:21 +1100405 /* Check for immediate EOF on stdin. */
406 len = read(fileno(stdin), buf, 1);
407 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000408 /*
409 * EOF. Record that we have seen it and send
410 * EOF to server.
411 */
Damien Miller95def091999-11-25 00:26:21 +1100412 debug("Sending eof.");
413 stdin_eof = 1;
414 packet_start(SSH_CMSG_EOF);
415 packet_send();
416 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100417 /*
418 * Got data. We must store the data in the buffer,
419 * and also process it as an escape character if
420 * appropriate.
421 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000422 if ((u_char) buf[0] == escape_char1)
423 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000424 else
Damien Miller95def091999-11-25 00:26:21 +1100425 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100426 }
Damien Miller95def091999-11-25 00:26:21 +1100427 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429}
430
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller5428f641999-11-25 11:54:57 +1100432/*
433 * Make packets from buffered stdin data, and buffer them for sending to the
434 * connection.
435 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Ben Lindstrombba81212001-06-25 05:01:22 +0000437static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000438client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000440 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Damien Miller95def091999-11-25 00:26:21 +1100442 /* Send buffered stdin data to the server. */
443 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100444 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100445 len = buffer_len(&stdin_buffer);
446 /* Keep the packets at reasonable size. */
447 if (len > packet_get_maxsize())
448 len = packet_get_maxsize();
449 packet_start(SSH_CMSG_STDIN_DATA);
450 packet_put_string(buffer_ptr(&stdin_buffer), len);
451 packet_send();
452 buffer_consume(&stdin_buffer, len);
453 /* If we have a pending EOF, send it now. */
454 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
455 packet_start(SSH_CMSG_EOF);
456 packet_send();
457 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459}
460
Damien Miller5428f641999-11-25 11:54:57 +1100461/*
462 * Checks if the client window has changed, and sends a packet about it to
463 * the server if so. The actual change is detected elsewhere (by a software
464 * interrupt on Unix); this just checks the flag and sends a message if
465 * appropriate.
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_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470{
Damien Miller1383bd82000-04-06 12:32:37 +1000471 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Damien Miller1383bd82000-04-06 12:32:37 +1000473 if (! received_window_change_signal)
474 return;
475 /** XXX race */
476 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Millerd3444942000-09-30 14:20:03 +1100478 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000479
480 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000481 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000482 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000483 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
484 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000485 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100486 packet_put_int((u_int)ws.ws_row);
487 packet_put_int((u_int)ws.ws_col);
488 packet_put_int((u_int)ws.ws_xpixel);
489 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000490 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492}
493
Damien Miller509b0102003-12-17 16:33:10 +1100494static void
495client_global_request_reply(int type, u_int32_t seq, void *ctxt)
496{
Darren Tucker9f407c42008-06-13 04:50:27 +1000497 struct global_confirm *gc;
498
499 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
500 return;
501 if (gc->cb != NULL)
502 gc->cb(type, seq, gc->ctx);
503 if (--gc->ref_count <= 0) {
504 TAILQ_REMOVE(&global_confirms, gc, entry);
505 bzero(gc, sizeof(*gc));
506 xfree(gc);
507 }
508
Darren Tuckerf7288d72009-06-21 18:12:20 +1000509 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100510}
511
512static void
513server_alive_check(void)
514{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000515 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000516 logit("Timeout, server not responding.");
517 cleanup_exit(255);
518 }
Damien Miller509b0102003-12-17 16:33:10 +1100519 packet_start(SSH2_MSG_GLOBAL_REQUEST);
520 packet_put_cstring("keepalive@openssh.com");
521 packet_put_char(1); /* boolean: want reply */
522 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000523 /* Insert an empty placeholder to maintain ordering */
524 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100525}
526
Damien Miller5428f641999-11-25 11:54:57 +1100527/*
528 * Waits until the client can do something (some data becomes available on
529 * one of the file descriptors).
530 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Miller5e953212001-01-30 09:14:00 +1100532client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000533 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534{
Damien Miller509b0102003-12-17 16:33:10 +1100535 struct timeval tv, *tvp;
536 int ret;
537
Damien Miller5e953212001-01-30 09:14:00 +1100538 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000539 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller1383bd82000-04-06 12:32:37 +1000541 if (!compat20) {
542 /* Read from the connection, unless our buffers are full. */
543 if (buffer_len(&stdout_buffer) < buffer_high &&
544 buffer_len(&stderr_buffer) < buffer_high &&
545 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100546 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000547 /*
548 * Read from stdin, unless we have seen EOF or have very much
549 * buffered data to send to the server.
550 */
551 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100552 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000553
554 /* Select stdout/stderr if have data in buffer. */
555 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100556 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000557 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100558 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000559 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000560 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000561 if (session_closed && !channel_still_open() &&
562 !packet_have_data_to_write()) {
563 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100564 memset(*readsetp, 0, *nallocp);
565 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000566 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000567 } else {
568 FD_SET(connection_in, *readsetp);
569 }
Damien Miller1383bd82000-04-06 12:32:37 +1000570 }
571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Select server connection if have data to write to the server. */
573 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100574 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100575
Damien Miller5428f641999-11-25 11:54:57 +1100576 /*
577 * Wait for something to happen. This will suspend the process until
578 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100579 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100580 */
Damien Miller95def091999-11-25 00:26:21 +1100581
Damien Miller509b0102003-12-17 16:33:10 +1100582 if (options.server_alive_interval == 0 || !compat20)
583 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000584 else {
Damien Miller509b0102003-12-17 16:33:10 +1100585 tv.tv_sec = options.server_alive_interval;
586 tv.tv_usec = 0;
587 tvp = &tv;
588 }
589 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
590 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100591 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000592
593 /*
594 * We have to clear the select masks, because we return.
595 * We have to return, because the mainloop checks for the flags
596 * set by the signal handlers.
597 */
Damien Miller79faeff2001-11-12 11:06:32 +1100598 memset(*readsetp, 0, *nallocp);
599 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000600
Damien Miller95def091999-11-25 00:26:21 +1100601 if (errno == EINTR)
602 return;
603 /* Note: we might still have data in the buffers. */
604 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
605 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100606 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100607 } else if (ret == 0)
608 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609}
610
Ben Lindstrombba81212001-06-25 05:01:22 +0000611static void
Damien Millerad833b32000-08-23 10:46:23 +1000612client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613{
Damien Miller95def091999-11-25 00:26:21 +1100614 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000615 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000616 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
617 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000618 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000619 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
620 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100622 leave_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller5428f641999-11-25 11:54:57 +1100624 /*
625 * Free (and clear) the buffer to reduce the amount of data that gets
626 * written to swap.
627 */
Damien Millerad833b32000-08-23 10:46:23 +1000628 buffer_free(bin);
629 buffer_free(bout);
630 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller95def091999-11-25 00:26:21 +1100632 /* Send the suspend signal to the program itself. */
633 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Darren Tucker5d78de62004-11-05 20:35:44 +1100635 /* Reset window sizes in case they have changed */
636 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
Damien Miller95def091999-11-25 00:26:21 +1100638 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000639 buffer_init(bin);
640 buffer_init(bout);
641 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100643 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644}
645
Ben Lindstrombba81212001-06-25 05:01:22 +0000646static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100647client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000649 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000650 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651
Damien Miller5428f641999-11-25 11:54:57 +1100652 /*
653 * Read input from the server, and add any such data to the buffer of
654 * the packet subsystem.
655 */
Damien Miller95def091999-11-25 00:26:21 +1100656 if (FD_ISSET(connection_in, readset)) {
657 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000658 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
659 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000660 /*
661 * Received EOF. The remote host has closed the
662 * connection.
663 */
664 snprintf(buf, sizeof buf,
665 "Connection to %.300s closed by remote host.\r\n",
666 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668 quit_pending = 1;
669 return;
Damien Miller95def091999-11-25 00:26:21 +1100670 }
Damien Miller5428f641999-11-25 11:54:57 +1100671 /*
672 * There is a kernel bug on Solaris that causes select to
673 * sometimes wake up even though there is no data available.
674 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000675 if (len < 0 &&
676 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100677 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
Damien Miller95def091999-11-25 00:26:21 +1100679 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000680 /*
681 * An error has encountered. Perhaps there is a
682 * network problem.
683 */
684 snprintf(buf, sizeof buf,
685 "Read from remote host %.300s: %.100s\r\n",
686 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100687 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100688 quit_pending = 1;
689 return;
690 }
691 packet_process_incoming(buf, len);
692 }
Damien Miller1383bd82000-04-06 12:32:37 +1000693}
694
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000695static void
Damien Miller5771ed72008-05-19 15:35:33 +1000696client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000697{
Damien Miller5771ed72008-05-19 15:35:33 +1000698 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
699 char errmsg[256];
700 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000701
Damien Miller5771ed72008-05-19 15:35:33 +1000702 /* XXX supress on mux _client_ quietmode */
703 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100704 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000705
Damien Miller5771ed72008-05-19 15:35:33 +1000706 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
707 debug2("%s request accepted on channel %d",
708 cr->request_type, c->self);
709 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
710 if (tochan) {
711 snprintf(errmsg, sizeof(errmsg),
712 "%s request failed\r\n", cr->request_type);
713 } else {
714 snprintf(errmsg, sizeof(errmsg),
715 "%s request failed on channel %d",
716 cr->request_type, c->self);
717 }
718 /* If error occurred on primary session channel, then exit */
719 if (cr->do_close && c->self == session_ident)
720 fatal("%s", errmsg);
721 /* If error occurred on mux client, append to their stderr */
722 if (tochan)
723 buffer_append(&c->extended, errmsg, strlen(errmsg));
724 else
725 error("%s", errmsg);
726 if (cr->do_close) {
727 chan_read_failed(c);
728 chan_write_failed(c);
729 }
Damien Miller0e220db2004-06-15 10:34:08 +1000730 }
Damien Miller5771ed72008-05-19 15:35:33 +1000731 xfree(cr);
732}
Damien Miller0e220db2004-06-15 10:34:08 +1000733
Damien Miller5771ed72008-05-19 15:35:33 +1000734static void
735client_abandon_status_confirm(Channel *c, void *ctx)
736{
737 xfree(ctx);
738}
739
740static void
741client_expect_confirm(int id, const char *request, int do_close)
742{
743 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
744
745 cr->request_type = request;
746 cr->do_close = do_close;
747
748 channel_register_status_confirm(id, client_status_confirm,
749 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000750}
751
Darren Tucker9f407c42008-06-13 04:50:27 +1000752void
753client_register_global_confirm(global_confirm_cb *cb, void *ctx)
754{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000755 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000756
757 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000758 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
759 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
760 if (++last_gc->ref_count >= INT_MAX)
761 fatal("%s: last_gc->ref_count = %d",
762 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000763 return;
764 }
765
766 gc = xmalloc(sizeof(*gc));
767 gc->cb = cb;
768 gc->ctx = ctx;
769 gc->ref_count = 1;
770 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
771}
772
Damien Miller0e220db2004-06-15 10:34:08 +1000773static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000774process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000775{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000776 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100777 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000778 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100779 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100780 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100781 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000782
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100783 bzero(&fwd, sizeof(fwd));
784 fwd.listen_host = fwd.connect_host = NULL;
785
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100786 leave_raw_mode(force_tty_flag);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000787 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000788 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000789 if (s == NULL)
790 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100791 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000792 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000793 if (*s == '-')
794 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000795 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000796 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000797
Darren Tucker1973c882004-05-24 10:34:36 +1000798 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000799 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000800 logit(" -L[bind_address:]port:host:hostport "
801 "Request local forward");
802 logit(" -R[bind_address:]port:host:hostport "
803 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100804 logit(" -D[bind_address:]port "
805 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000806 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000807 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100808 if (!options.permit_local_command)
809 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000810 logit(" !args "
811 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100812 goto out;
813 }
814
815 if (*s == '!' && options.permit_local_command) {
816 s++;
817 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000818 goto out;
819 }
820
821 if (*s == 'K') {
822 delete = 1;
823 s++;
824 }
Damien Miller0164cb82008-11-05 16:30:31 +1100825 if (*s == 'L')
826 local = 1;
827 else if (*s == 'R')
828 remote = 1;
829 else if (*s == 'D')
830 dynamic = 1;
831 else {
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000833 goto out;
834 }
Damien Miller0164cb82008-11-05 16:30:31 +1100835
836 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000837 logit("Not supported.");
838 goto out;
839 }
Damien Miller0164cb82008-11-05 16:30:31 +1100840 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000841 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000842 goto out;
843 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000844
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100845 while (isspace(*++s))
846 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000847
Damien Millere1537f92010-01-26 13:26:22 +1100848 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000849 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100850 cancel_port = 0;
851 cancel_host = hpdelim(&s); /* may be NULL */
852 if (s != NULL) {
853 cancel_port = a2port(s);
854 cancel_host = cleanhostname(cancel_host);
855 } else {
856 cancel_port = a2port(cancel_host);
857 cancel_host = NULL;
858 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100859 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100860 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000861 goto out;
862 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100863 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000864 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100865 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000866 logit("Bad forwarding specification.");
867 goto out;
868 }
Damien Miller0164cb82008-11-05 16:30:31 +1100869 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100870 if (channel_setup_local_fwd_listener(fwd.listen_host,
871 fwd.listen_port, fwd.connect_host,
872 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000873 logit("Port forwarding failed.");
874 goto out;
875 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100876 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000877 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100878 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000879 fwd.connect_port) < 0) {
880 logit("Port forwarding failed.");
881 goto out;
882 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100883 }
884
Darren Tuckere7066df2004-05-24 10:18:05 +1000885 logit("Forwarding port.");
886 }
887
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000888out:
889 signal(SIGINT, handler);
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100890 enter_raw_mode(force_tty_flag);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000891 if (cmd)
892 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100893 if (fwd.listen_host != NULL)
894 xfree(fwd.listen_host);
895 if (fwd.connect_host != NULL)
896 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000897}
898
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000899/*
900 * Process the characters one by one, call with c==NULL for proto1 case.
901 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000903process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
904 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000905{
906 char string[1024];
907 pid_t pid;
908 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000909 u_int i;
910 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000911 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000912 int *escape_pendingp, escape_char;
913 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000914
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000915 if (c == NULL) {
916 escape_pendingp = &escape_pending1;
917 escape_char = escape_char1;
918 } else {
919 if (c->filter_ctx == NULL)
920 return 0;
921 efc = (struct escape_filter_ctx *)c->filter_ctx;
922 escape_pendingp = &efc->escape_pending;
923 escape_char = efc->escape_char;
924 }
925
Damien Millereccb9de2005-06-17 12:59:34 +1000926 if (len <= 0)
927 return (0);
928
929 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000930 /* Get one character at a time. */
931 ch = buf[i];
932
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000933 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000934 /* We have previously seen an escape character. */
935 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000936 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000937
938 /* Process the escaped character. */
939 switch (ch) {
940 case '.':
941 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000942 snprintf(string, sizeof string, "%c.\r\n",
943 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000944 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000945
Damien Millere1537f92010-01-26 13:26:22 +1100946 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000947 chan_read_failed(c);
948 chan_write_failed(c);
949 return 0;
950 } else
951 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000952 return -1;
953
954 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000955 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100956 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000957 noescape:
958 snprintf(string, sizeof string,
959 "%c%c escape not available to "
960 "multiplexed sessions\r\n",
961 escape_char, ch);
962 buffer_append(berr, string,
963 strlen(string));
964 continue;
965 }
Darren Tucker4d5cd332008-06-13 04:51:14 +1000966 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000967 snprintf(string, sizeof string,
968 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000969 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000970
971 /* Restore terminal modes and suspend. */
972 client_suspend_self(bin, bout, berr);
973
974 /* We have been continued. */
975 continue;
976
Damien Miller54c45982003-05-15 10:20:13 +1000977 case 'B':
978 if (compat20) {
979 snprintf(string, sizeof string,
980 "%cB\r\n", escape_char);
981 buffer_append(berr, string,
982 strlen(string));
983 channel_request_start(session_ident,
984 "break", 0);
985 packet_put_int(1000);
986 packet_send();
987 }
988 continue;
989
Ben Lindstromf28f6342001-04-04 02:03:04 +0000990 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000991 if (compat20) {
992 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000993 logit("Server does not "
994 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000995 else
996 need_rekeying = 1;
997 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000998 continue;
999
Damien Millerad833b32000-08-23 10:46:23 +10001000 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001001 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001002 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001003 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001004 * Detach the program (continue to serve
1005 * connections, but put in background and no
1006 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001007 */
Damien Miller96507ef2001-11-12 10:52:25 +11001008 /* Restore tty modes. */
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001009 leave_raw_mode(force_tty_flag);
Damien Miller96507ef2001-11-12 10:52:25 +11001010
1011 /* Stop listening for new connections. */
1012 channel_stop_listening();
1013
1014 snprintf(string, sizeof string,
1015 "%c& [backgrounded]\n", escape_char);
1016 buffer_append(berr, string, strlen(string));
1017
1018 /* Fork into background. */
1019 pid = fork();
1020 if (pid < 0) {
1021 error("fork: %.100s", strerror(errno));
1022 continue;
1023 }
1024 if (pid != 0) { /* This is the parent. */
1025 /* The parent just exits. */
1026 exit(0);
1027 }
1028 /* The child continues serving connections. */
1029 if (compat20) {
1030 buffer_append(bin, "\004", 1);
1031 /* fake EOF on stdin */
1032 return -1;
1033 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001034 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001035 * Sending SSH_CMSG_EOF alone does not
1036 * always appear to be enough. So we
1037 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001038 */
1039 packet_start(SSH_CMSG_STDIN_DATA);
1040 packet_put_string("\004", 1);
1041 packet_send();
1042 /* Close stdin. */
1043 stdin_eof = 1;
1044 if (buffer_len(bin) == 0) {
1045 packet_start(SSH_CMSG_EOF);
1046 packet_send();
1047 }
1048 }
Damien Miller96507ef2001-11-12 10:52:25 +11001049 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001050
1051 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001052 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001053 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001054"%c?\r\n\
1055Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001056 %c. - terminate session\r\n\
1057 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001058 %cR - Request rekey (SSH protocol 2 only)\r\n\
1059 %c# - list forwarded connections\r\n\
1060 %c? - this message\r\n\
1061 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001062(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001063 escape_char, escape_char,
1064 escape_char, escape_char,
1065 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001066 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001067 } else {
1068 snprintf(string, sizeof string,
1069"%c?\r\n\
1070Supported escape sequences:\r\n\
1071 %c. - terminate connection (and any multiplexed sessions)\r\n\
1072 %cB - send a BREAK to the remote system\r\n\
1073 %cC - open a command line\r\n\
1074 %cR - Request rekey (SSH protocol 2 only)\r\n\
1075 %c^Z - suspend ssh\r\n\
1076 %c# - list forwarded connections\r\n\
1077 %c& - background ssh (when waiting for connections to terminate)\r\n\
1078 %c? - this message\r\n\
1079 %c%c - send the escape character by typing it twice\r\n\
1080(Note that escapes are only recognized immediately after newline.)\r\n",
1081 escape_char, escape_char,
1082 escape_char, escape_char,
1083 escape_char, escape_char,
1084 escape_char, escape_char,
1085 escape_char, escape_char,
1086 escape_char);
1087 }
Damien Millerad833b32000-08-23 10:46:23 +10001088 buffer_append(berr, string, strlen(string));
1089 continue;
1090
1091 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001092 snprintf(string, sizeof string, "%c#\r\n",
1093 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001094 buffer_append(berr, string, strlen(string));
1095 s = channel_open_message();
1096 buffer_append(berr, s, strlen(s));
1097 xfree(s);
1098 continue;
1099
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001100 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001101 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001102 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001103 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001104 continue;
1105
Damien Millerad833b32000-08-23 10:46:23 +10001106 default:
1107 if (ch != escape_char) {
1108 buffer_put_char(bin, escape_char);
1109 bytes++;
1110 }
1111 /* Escaped characters fall through here */
1112 break;
1113 }
1114 } else {
1115 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001116 * The previous character was not an escape char.
1117 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001118 */
1119 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001120 /*
1121 * It is. Set the flag and continue to
1122 * next character.
1123 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001124 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001125 continue;
1126 }
1127 }
1128
1129 /*
1130 * Normal character. Record whether it was a newline,
1131 * and append it to the buffer.
1132 */
1133 last_was_cr = (ch == '\r' || ch == '\n');
1134 buffer_put_char(bin, ch);
1135 bytes++;
1136 }
1137 return bytes;
1138}
1139
Ben Lindstrombba81212001-06-25 05:01:22 +00001140static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001141client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001142{
Damien Miller166fca82000-04-20 07:42:21 +10001143 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001144 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001145
Damien Miller95def091999-11-25 00:26:21 +11001146 /* Read input from stdin. */
1147 if (FD_ISSET(fileno(stdin), readset)) {
1148 /* Read as much as possible. */
1149 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001150 if (len < 0 &&
1151 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001152 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001153 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001154 /*
1155 * Received EOF or error. They are treated
1156 * similarly, except that an error message is printed
1157 * if it was an error condition.
1158 */
Damien Miller95def091999-11-25 00:26:21 +11001159 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001160 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1161 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001162 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001163 }
1164 /* Mark that we have seen EOF. */
1165 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001166 /*
1167 * Send an EOF message to the server unless there is
1168 * data in the buffer. If there is data in the
1169 * buffer, no message will be sent now. Code
1170 * elsewhere will send the EOF when the buffer
1171 * becomes empty if stdin_eof is set.
1172 */
Damien Miller95def091999-11-25 00:26:21 +11001173 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 packet_start(SSH_CMSG_EOF);
1175 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001176 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001177 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001178 /*
1179 * Normal successful read, and no escape character.
1180 * Just append the data to buffer.
1181 */
Damien Miller95def091999-11-25 00:26:21 +11001182 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001183 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001184 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001185 * Normal, successful read. But we have an escape
1186 * character and have to process the characters one
1187 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001188 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001189 if (process_escapes(NULL, &stdin_buffer,
1190 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001191 return;
Damien Miller95def091999-11-25 00:26:21 +11001192 }
1193 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194}
1195
Ben Lindstrombba81212001-06-25 05:01:22 +00001196static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001197client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198{
Damien Miller95def091999-11-25 00:26:21 +11001199 int len;
1200 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 /* Write buffered output to stdout. */
1203 if (FD_ISSET(fileno(stdout), writeset)) {
1204 /* Write as much data as possible. */
1205 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001206 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001207 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001208 if (errno == EINTR || errno == EAGAIN ||
1209 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001210 len = 0;
1211 else {
Damien Miller5428f641999-11-25 11:54:57 +11001212 /*
1213 * An error or EOF was encountered. Put an
1214 * error message to stderr buffer.
1215 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001216 snprintf(buf, sizeof buf,
1217 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001218 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001219 quit_pending = 1;
1220 return;
1221 }
1222 }
1223 /* Consume printed data from the buffer. */
1224 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 }
Damien Miller95def091999-11-25 00:26:21 +11001226 /* Write buffered output to stderr. */
1227 if (FD_ISSET(fileno(stderr), writeset)) {
1228 /* Write as much data as possible. */
1229 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001230 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001231 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001232 if (errno == EINTR || errno == EAGAIN ||
1233 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001234 len = 0;
1235 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001236 /*
1237 * EOF or error, but can't even print
1238 * error message.
1239 */
Damien Miller95def091999-11-25 00:26:21 +11001240 quit_pending = 1;
1241 return;
1242 }
1243 }
1244 /* Consume printed characters from the buffer. */
1245 buffer_consume(&stderr_buffer, len);
1246 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
1248
Damien Miller5428f641999-11-25 11:54:57 +11001249/*
Damien Millerb38eff82000-04-01 11:09:21 +10001250 * Get packets from the connection input buffer, and process them as long as
1251 * there are packets available.
1252 *
1253 * Any unknown packets received during the actual
1254 * session cause the session to terminate. This is
1255 * intended to make debugging easier since no
1256 * confirmations are sent. Any compatible protocol
1257 * extensions must be negotiated during the
1258 * preparatory phase.
1259 */
1260
Ben Lindstrombba81212001-06-25 05:01:22 +00001261static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001262client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001263{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001264 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1265 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001266}
1267
Damien Millerad833b32000-08-23 10:46:23 +10001268/* scan buf[] for '~' before sending data to the peer */
1269
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001270/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1271void *
1272client_new_escape_filter_ctx(int escape_char)
1273{
1274 struct escape_filter_ctx *ret;
1275
1276 ret = xmalloc(sizeof(*ret));
1277 ret->escape_pending = 0;
1278 ret->escape_char = escape_char;
1279 return (void *)ret;
1280}
1281
Darren Tucker84c56f52008-06-13 04:55:46 +10001282/* Free the escape filter context on channel free */
1283void
1284client_filter_cleanup(int cid, void *ctx)
1285{
1286 xfree(ctx);
1287}
1288
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001289int
1290client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001291{
Damien Miller5771ed72008-05-19 15:35:33 +10001292 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1293 return 0;
1294
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001295 return process_escapes(c, &c->input, &c->output, &c->extended,
1296 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001297}
1298
Ben Lindstrombba81212001-06-25 05:01:22 +00001299static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001300client_channel_closed(int id, void *arg)
1301{
Damien Miller3ec27592001-10-12 11:35:04 +10001302 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001303 session_closed = 1;
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001304 leave_raw_mode(force_tty_flag);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001305}
1306
Damien Millerb38eff82000-04-01 11:09:21 +10001307/*
Damien Miller5428f641999-11-25 11:54:57 +11001308 * Implements the interactive session with the server. This is called after
1309 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001310 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1311 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001312 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313
Damien Miller4af51302000-04-16 11:18:38 +10001314int
Damien Millerad833b32000-08-23 10:46:23 +10001315client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316{
Damien Miller5e953212001-01-30 09:14:00 +11001317 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001318 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001319 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001320 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001321 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001322 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323
Damien Miller95def091999-11-25 00:26:21 +11001324 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001325
Damien Miller95def091999-11-25 00:26:21 +11001326 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327
Damien Miller95def091999-11-25 00:26:21 +11001328 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001329 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001330 last_was_cr = 1;
1331 exit_status = -1;
1332 stdin_eof = 0;
1333 buffer_high = 64 * 1024;
1334 connection_in = packet_get_connection_in();
1335 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001336 max_fd = MAX(connection_in, connection_out);
1337
1338 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001339 /* enable nonblocking unless tty */
1340 if (!isatty(fileno(stdin)))
1341 set_nonblock(fileno(stdin));
1342 if (!isatty(fileno(stdout)))
1343 set_nonblock(fileno(stdout));
1344 if (!isatty(fileno(stderr)))
1345 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001346 max_fd = MAX(max_fd, fileno(stdin));
1347 max_fd = MAX(max_fd, fileno(stdout));
1348 max_fd = MAX(max_fd, fileno(stderr));
1349 }
Damien Miller95def091999-11-25 00:26:21 +11001350 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001351 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352
Damien Miller95def091999-11-25 00:26:21 +11001353 /* Initialize buffers. */
1354 buffer_init(&stdin_buffer);
1355 buffer_init(&stdout_buffer);
1356 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
Damien Millerb38eff82000-04-01 11:09:21 +10001358 client_init_dispatch();
1359
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001360 /*
1361 * Set signal handlers, (e.g. to restore non-blocking mode)
1362 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1363 */
Darren Tucker07336da2004-11-05 20:02:16 +11001364 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1365 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001366 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1367 signal(SIGINT, signal_handler);
1368 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1369 signal(SIGQUIT, signal_handler);
1370 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1371 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001372 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373
Damien Miller95def091999-11-25 00:26:21 +11001374 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001375 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376
Ben Lindstrom5b828322001-02-09 01:34:36 +00001377 if (compat20) {
1378 session_ident = ssh2_chan_id;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001379 if (escape_char_arg != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001380 channel_register_filter(session_ident,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001381 client_simple_escape_filter, NULL,
Darren Tucker84c56f52008-06-13 04:55:46 +10001382 client_filter_cleanup,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001383 client_new_escape_filter_ctx(escape_char_arg));
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001384 if (session_ident != -1)
1385 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001386 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001387 } else {
1388 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001389 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001390 }
Damien Millerad833b32000-08-23 10:46:23 +10001391
Damien Miller95def091999-11-25 00:26:21 +11001392 /* Main loop of the client for the interactive session mode. */
1393 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394
Damien Miller5428f641999-11-25 11:54:57 +11001395 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001396 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001398 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001399 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001400
1401 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001402
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001403 if (rekeying) {
1404 debug("rekeying in progress");
1405 } else {
1406 /*
1407 * Make packets of buffered stdin data, and buffer
1408 * them for sending to the server.
1409 */
1410 if (!compat20)
1411 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001413 /*
1414 * Make packets from buffered channel data, and
1415 * enqueue them for sending to the server.
1416 */
1417 if (packet_not_very_much_data_to_write())
1418 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001420 /*
1421 * Check if the window size has changed, and buffer a
1422 * message about it to the server if so.
1423 */
1424 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001426 if (quit_pending)
1427 break;
1428 }
Damien Miller5428f641999-11-25 11:54:57 +11001429 /*
1430 * Wait until we have something to do (something becomes
1431 * available on one of the descriptors).
1432 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001433 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001434 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001435 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436
Damien Miller95def091999-11-25 00:26:21 +11001437 if (quit_pending)
1438 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001440 /* Do channel operations unless rekeying in progress. */
1441 if (!rekeying) {
1442 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001443 if (need_rekeying || packet_need_rekeying()) {
1444 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001445 xxx_kex->done = 0;
1446 kex_send_kexinit(xxx_kex);
1447 need_rekeying = 0;
1448 }
1449 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450
Damien Miller1383bd82000-04-06 12:32:37 +10001451 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001452 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Damien Miller1383bd82000-04-06 12:32:37 +10001454 if (quit_pending)
1455 break;
1456
1457 if (!compat20) {
1458 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001459 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001460 /*
1461 * Process output to stdout and stderr. Output to
1462 * the connection is processed elsewhere (above).
1463 */
Damien Miller5e953212001-01-30 09:14:00 +11001464 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466
Darren Tuckere32cf432010-01-08 16:51:40 +11001467 if (session_resumed) {
1468 connection_in = packet_get_connection_in();
1469 connection_out = packet_get_connection_out();
1470 max_fd = MAX(max_fd, connection_out);
1471 max_fd = MAX(max_fd, connection_in);
1472 session_resumed = 0;
1473 }
1474
Darren Tucker4d5cd332008-06-13 04:51:14 +10001475 /*
1476 * Send as much buffered packet data as possible to the
1477 * sender.
1478 */
Damien Miller5e953212001-01-30 09:14:00 +11001479 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001480 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481 }
Damien Miller5e953212001-01-30 09:14:00 +11001482 if (readset)
1483 xfree(readset);
1484 if (writeset)
1485 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486
Damien Miller95def091999-11-25 00:26:21 +11001487 /* Terminate the session. */
1488
1489 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001490 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001491
Darren Tuckerde0c0252009-07-06 07:17:35 +10001492 if (compat20) {
1493 packet_start(SSH2_MSG_DISCONNECT);
1494 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1495 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001496 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001497 packet_send();
1498 packet_write_wait();
1499 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001500
Ben Lindstrom601e4362001-06-21 03:19:23 +00001501 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001502
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001503 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001504 leave_raw_mode(force_tty_flag);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001505
1506 /* restore blocking io */
1507 if (!isatty(fileno(stdin)))
1508 unset_nonblock(fileno(stdin));
1509 if (!isatty(fileno(stdout)))
1510 unset_nonblock(fileno(stdout));
1511 if (!isatty(fileno(stderr)))
1512 unset_nonblock(fileno(stderr));
1513
Damien Millerd6965512003-12-17 16:31:53 +11001514 /*
1515 * If there was no shell or command requested, there will be no remote
1516 * exit status to be returned. In that case, clear error code if the
1517 * connection was deliberately terminated at this end.
1518 */
1519 if (no_shell_flag && received_signal == SIGTERM) {
1520 received_signal = 0;
1521 exit_status = 0;
1522 }
1523
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001524 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001525 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001526
1527 /*
1528 * In interactive mode (with pseudo tty) display a message indicating
1529 * that the connection has been closed.
1530 */
1531 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001532 snprintf(buf, sizeof buf,
1533 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001534 buffer_append(&stderr_buffer, buf, strlen(buf));
1535 }
1536
Damien Miller95def091999-11-25 00:26:21 +11001537 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001538 while (buffer_len(&stdout_buffer) > 0) {
1539 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001540 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001541 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001542 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001543 break;
1544 }
Damien Miller95def091999-11-25 00:26:21 +11001545 buffer_consume(&stdout_buffer, len);
1546 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547
Damien Miller95def091999-11-25 00:26:21 +11001548 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001549 while (buffer_len(&stderr_buffer) > 0) {
1550 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001551 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001552 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001553 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001554 break;
1555 }
Damien Miller95def091999-11-25 00:26:21 +11001556 buffer_consume(&stderr_buffer, len);
1557 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001558
Damien Miller95def091999-11-25 00:26:21 +11001559 /* Clear and free any buffers. */
1560 memset(buf, 0, sizeof(buf));
1561 buffer_free(&stdin_buffer);
1562 buffer_free(&stdout_buffer);
1563 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Damien Miller95def091999-11-25 00:26:21 +11001565 /* Report bytes transferred, and transfer rates. */
1566 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001567 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1568 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1569 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1570 obytes, ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001571 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001572 verbose("Bytes per second: sent %.1f, received %.1f",
1573 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001574 /* Return the exit status of the program. */
1575 debug("Exit status %d", exit_status);
1576 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577}
Damien Millerb38eff82000-04-01 11:09:21 +10001578
1579/*********/
1580
Ben Lindstrombba81212001-06-25 05:01:22 +00001581static void
Damien Miller630d6f42002-01-22 23:17:30 +11001582client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001583{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001584 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001585 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001586 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001587 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001588 memset(data, 0, data_len);
1589 xfree(data);
1590}
Ben Lindstrombba81212001-06-25 05:01:22 +00001591static void
Damien Miller630d6f42002-01-22 23:17:30 +11001592client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001593{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001594 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001595 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001596 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001597 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001598 memset(data, 0, data_len);
1599 xfree(data);
1600}
Ben Lindstrombba81212001-06-25 05:01:22 +00001601static void
Damien Miller630d6f42002-01-22 23:17:30 +11001602client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001603{
Damien Millerb38eff82000-04-01 11:09:21 +10001604 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001605 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001606 /* Acknowledge the exit. */
1607 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1608 packet_send();
1609 /*
1610 * Must wait for packet to be sent since we are
1611 * exiting the loop.
1612 */
1613 packet_write_wait();
1614 /* Flag that we want to exit. */
1615 quit_pending = 1;
1616}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001617static void
1618client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1619{
1620 Channel *c = NULL;
1621 int remote_id, sock;
1622
1623 /* Read the remote channel number from the message. */
1624 remote_id = packet_get_int();
1625 packet_check_eom();
1626
1627 /*
1628 * Get a connection to the local authentication agent (this may again
1629 * get forwarded).
1630 */
1631 sock = ssh_get_authentication_socket();
1632
1633 /*
1634 * If we could not connect the agent, send an error message back to
1635 * the server. This should never happen unless the agent dies,
1636 * because authentication forwarding is only enabled if we have an
1637 * agent.
1638 */
1639 if (sock >= 0) {
1640 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1641 -1, 0, 0, 0, "authentication agent connection", 1);
1642 c->remote_id = remote_id;
1643 c->force_drain = 1;
1644 }
1645 if (c == NULL) {
1646 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1647 packet_put_int(remote_id);
1648 } else {
1649 /* Send a confirmation to the remote host. */
1650 debug("Forwarding authentication connection.");
1651 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1652 packet_put_int(remote_id);
1653 packet_put_int(c->self);
1654 }
1655 packet_send();
1656}
Damien Millerb38eff82000-04-01 11:09:21 +10001657
Ben Lindstrombba81212001-06-25 05:01:22 +00001658static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001659client_request_forwarded_tcpip(const char *request_type, int rchan)
1660{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001661 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001663 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001664
1665 /* Get rest of the packet */
1666 listen_address = packet_get_string(NULL);
1667 listen_port = packet_get_int();
1668 originator_address = packet_get_string(NULL);
1669 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001670 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001671
Damien Millerbd740252008-05-19 15:37:09 +10001672 debug("client_request_forwarded_tcpip: listen %s port %d, "
1673 "originator %s port %d", listen_address, listen_port,
1674 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001675
Damien Millerbd740252008-05-19 15:37:09 +10001676 c = channel_connect_by_listen_address(listen_port,
1677 "forwarded-tcpip", originator_address);
1678
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679 xfree(originator_address);
1680 xfree(listen_address);
1681 return c;
1682}
1683
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001684static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685client_request_x11(const char *request_type, int rchan)
1686{
1687 Channel *c = NULL;
1688 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001689 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001690 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001691
1692 if (!options.forward_x11) {
1693 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001694 error("Warning: this is probably a break-in attempt by a "
1695 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001696 return NULL;
1697 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001698 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1699 verbose("Rejected X11 connection after ForwardX11Timeout "
1700 "expired");
1701 return NULL;
1702 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001703 originator = packet_get_string(NULL);
1704 if (datafellows & SSH_BUG_X11FWD) {
1705 debug2("buggy server: x11 request w/o originator_port");
1706 originator_port = 0;
1707 } else {
1708 originator_port = packet_get_int();
1709 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001710 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001711 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001712 debug("client_request_x11: request from %s %d", originator,
1713 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001714 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001715 sock = x11_connect_display();
1716 if (sock < 0)
1717 return NULL;
1718 c = channel_new("x11",
1719 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001720 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001721 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001722 return c;
1723}
1724
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001725static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001726client_request_agent(const char *request_type, int rchan)
1727{
1728 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001729 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001730
1731 if (!options.forward_agent) {
1732 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001733 error("Warning: this is probably a break-in attempt by a "
1734 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001735 return NULL;
1736 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001737 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001738 if (sock < 0)
1739 return NULL;
1740 c = channel_new("authentication agent connection",
1741 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001742 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001743 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001744 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001745 return c;
1746}
1747
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001748int
1749client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1750{
1751 Channel *c;
1752 int fd;
1753
1754 if (tun_mode == SSH_TUNMODE_NO)
1755 return 0;
1756
1757 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001758 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001759 return -1;
1760 }
1761
1762 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1763
1764 /* Open local tunnel device */
1765 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1766 error("Tunnel device open failed.");
1767 return -1;
1768 }
1769
1770 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1771 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1772 c->datagram = 1;
1773
1774#if defined(SSH_TUN_FILTER)
1775 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1776 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001777 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001778#endif
1779
1780 packet_start(SSH2_MSG_CHANNEL_OPEN);
1781 packet_put_cstring("tun@openssh.com");
1782 packet_put_int(c->self);
1783 packet_put_int(c->local_window_max);
1784 packet_put_int(c->local_maxpacket);
1785 packet_put_int(tun_mode);
1786 packet_put_int(remote_tun);
1787 packet_send();
1788
1789 return 0;
1790}
1791
Damien Millerbd483e72000-04-30 10:00:53 +10001792/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001793static void
Damien Miller630d6f42002-01-22 23:17:30 +11001794client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001795{
1796 Channel *c = NULL;
1797 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001798 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001799 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001800
1801 ctype = packet_get_string(&len);
1802 rchan = packet_get_int();
1803 rwindow = packet_get_int();
1804 rmaxpack = packet_get_int();
1805
Damien Millere247cc42000-05-07 12:03:14 +10001806 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001807 ctype, rchan, rwindow, rmaxpack);
1808
Damien Miller0bc1bd82000-11-13 22:57:25 +11001809 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1810 c = client_request_forwarded_tcpip(ctype, rchan);
1811 } else if (strcmp(ctype, "x11") == 0) {
1812 c = client_request_x11(ctype, rchan);
1813 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1814 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001815 }
1816/* XXX duplicate : */
1817 if (c != NULL) {
1818 debug("confirm %s", ctype);
1819 c->remote_id = rchan;
1820 c->remote_window = rwindow;
1821 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001822 if (c->type != SSH_CHANNEL_CONNECTING) {
1823 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1824 packet_put_int(c->remote_id);
1825 packet_put_int(c->self);
1826 packet_put_int(c->local_window);
1827 packet_put_int(c->local_maxpacket);
1828 packet_send();
1829 }
Damien Millerbd483e72000-04-30 10:00:53 +10001830 } else {
1831 debug("failure %s", ctype);
1832 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1833 packet_put_int(rchan);
1834 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001835 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001836 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001837 packet_put_cstring("");
1838 }
Damien Millerbd483e72000-04-30 10:00:53 +10001839 packet_send();
1840 }
1841 xfree(ctype);
1842}
Ben Lindstrombba81212001-06-25 05:01:22 +00001843static void
Damien Miller630d6f42002-01-22 23:17:30 +11001844client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001845{
1846 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001847 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001848 char *rtype;
1849
1850 id = packet_get_int();
1851 rtype = packet_get_string(NULL);
1852 reply = packet_get_char();
1853
1854 debug("client_input_channel_req: channel %d rtype %s reply %d",
1855 id, rtype, reply);
1856
Damien Miller3bbd8782004-06-18 22:23:22 +10001857 if (id == -1) {
1858 error("client_input_channel_req: request for channel -1");
1859 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001860 error("client_input_channel_req: channel %d: "
1861 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001862 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1863 packet_check_eom();
1864 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001865 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001866 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001867 if (c->ctl_chan != -1) {
1868 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001869 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001870 } else if (id == session_ident) {
1871 /* Record exit value of local session */
1872 success = 1;
1873 exit_status = exitval;
1874 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001875 /* Probably for a mux channel that has already closed */
1876 debug("%s: no sink for exit-status on channel %d",
1877 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001878 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001879 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001880 }
1881 if (reply) {
1882 packet_start(success ?
1883 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001884 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001885 packet_send();
1886 }
1887 xfree(rtype);
1888}
Damien Millerc3fa4072002-01-22 23:21:58 +11001889static void
1890client_input_global_request(int type, u_int32_t seq, void *ctxt)
1891{
1892 char *rtype;
1893 int want_reply;
1894 int success = 0;
1895
1896 rtype = packet_get_string(NULL);
1897 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001898 debug("client_input_global_request: rtype %s want_reply %d",
1899 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001900 if (want_reply) {
1901 packet_start(success ?
1902 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1903 packet_send();
1904 packet_write_wait();
1905 }
1906 xfree(rtype);
1907}
Damien Millerbd483e72000-04-30 10:00:53 +10001908
Damien Miller0e220db2004-06-15 10:34:08 +10001909void
Darren Tuckerfc959702004-07-17 16:12:08 +10001910client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001911 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001912{
1913 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001914 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001915
1916 debug2("%s: id %d", __func__, id);
1917
Darren Tucker5d78de62004-11-05 20:35:44 +11001918 if ((c = channel_lookup(id)) == NULL)
1919 fatal("client_session2_setup: channel %d: unknown channel", id);
1920
Damien Miller0e220db2004-06-15 10:34:08 +10001921 if (want_tty) {
1922 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001923
1924 /* Store window size in the packet. */
1925 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1926 memset(&ws, 0, sizeof(ws));
1927
Damien Miller5771ed72008-05-19 15:35:33 +10001928 channel_request_start(id, "pty-req", 1);
Damien Miller22c97f12010-04-16 15:53:23 +10001929 client_expect_confirm(id, "PTY allocation", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001930 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001931 packet_put_int((u_int)ws.ws_col);
1932 packet_put_int((u_int)ws.ws_row);
1933 packet_put_int((u_int)ws.ws_xpixel);
1934 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001935 if (tiop == NULL)
1936 tiop = get_saved_tio();
1937 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001938 packet_send();
1939 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001940 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001941 }
1942
1943 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001944 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001945 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001946 char *name, *val;
1947
1948 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001949 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001950 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001951 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001952 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001953 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001954 continue;
1955 }
1956 *val++ = '\0';
1957
1958 matched = 0;
1959 for (j = 0; j < options.num_send_env; j++) {
1960 if (match_pattern(name, options.send_env[j])) {
1961 matched = 1;
1962 break;
1963 }
1964 }
1965 if (!matched) {
1966 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001967 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001968 continue;
1969 }
1970
1971 debug("Sending env %s = %s", name, val);
1972 channel_request_start(id, "env", 0);
1973 packet_put_cstring(name);
1974 packet_put_cstring(val);
1975 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001976 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001977 }
1978 }
1979
1980 len = buffer_len(cmd);
1981 if (len > 0) {
1982 if (len > 900)
1983 len = 900;
1984 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10001985 debug("Sending subsystem: %.*s",
1986 len, (u_char*)buffer_ptr(cmd));
1987 channel_request_start(id, "subsystem", 1);
1988 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001989 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001990 debug("Sending command: %.*s",
1991 len, (u_char*)buffer_ptr(cmd));
1992 channel_request_start(id, "exec", 1);
1993 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001994 }
1995 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1996 packet_send();
1997 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001998 channel_request_start(id, "shell", 1);
1999 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002000 packet_send();
2001 }
2002}
2003
Ben Lindstrombba81212001-06-25 05:01:22 +00002004static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002005client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002006{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002007 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002008
Damien Miller1383bd82000-04-06 12:32:37 +10002009 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2010 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2011 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2012 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002013 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002014 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2015 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002016 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002017 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002018 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2019 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002020 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002021
2022 /* rekeying */
2023 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002024
2025 /* global request reply messages */
2026 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2027 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002028}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002029
Ben Lindstrombba81212001-06-25 05:01:22 +00002030static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002031client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002032{
2033 dispatch_init(NULL);
2034 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2035 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2036 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002037 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2038 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2039 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002040 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2041 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2042 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002043
2044 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002045 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002046 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2047 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002048}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002049
Ben Lindstrombba81212001-06-25 05:01:22 +00002050static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002051client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002052{
2053 client_init_dispatch_13();
2054 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2055 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2056}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002057
Ben Lindstromdb47f382001-07-04 05:10:27 +00002058static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002059client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002060{
Damien Miller1383bd82000-04-06 12:32:37 +10002061 if (compat20)
2062 client_init_dispatch_20();
2063 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002064 client_init_dispatch_13();
2065 else
2066 client_init_dispatch_15();
2067}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002068
2069/* client specific fatal cleanup */
2070void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002071cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002072{
Darren Tucker37c1b3d2010-01-09 22:26:23 +11002073 leave_raw_mode(force_tty_flag);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002074 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002075 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002076 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002077 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002078}