blob: 1339521f47aee57d16094a722fb2298594ddc255 [file] [log] [blame]
Damien Millerff773642011-09-22 21:39:48 +10001/* $OpenBSD: clientloop.c,v 1.237 2011/09/10 22:26:34 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
103#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000104#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100105#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "authfd.h"
107#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000108#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000109#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "match.h"
111#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000112#include "roaming.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100113
114/* import options */
115extern Options options;
116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117/* Flag indicating that stdin should be redirected from /dev/null. */
118extern int stdin_null_flag;
119
Damien Millerd6965512003-12-17 16:31:53 +1100120/* Flag indicating that no shell has been requested */
121extern int no_shell_flag;
122
Damien Miller0e220db2004-06-15 10:34:08 +1000123/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100124extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000125
Damien Miller5428f641999-11-25 11:54:57 +1100126/*
127 * Name of the host we are connecting to. This is the name given on the
128 * command line, or the HostName specified for the user-supplied name in a
129 * configuration file.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131extern char *host;
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Flag to indicate that we have received a window change signal which has
135 * not yet been processed. This will cause a message indicating the new
136 * window size to be sent to the server a little later. This is volatile
137 * because this is updated in a signal handler.
138 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000139static volatile sig_atomic_t received_window_change_signal = 0;
140static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller788f2122005-11-05 15:14:59 +1100142/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143static int in_non_blocking_mode = 0;
144
Damien Millere11e1ea2010-08-03 16:04:46 +1000145/* Time when backgrounded control master using ControlPersist should exit */
146static time_t control_persist_exit_time = 0;
147
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100149volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000150static int escape_char1; /* Escape character. (proto1 only) */
151static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100152static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000153static int exit_status; /* Used to store the command exit status. */
154static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100155static Buffer stdin_buffer; /* Buffer for stdin data. */
156static Buffer stdout_buffer; /* Buffer for stdout data. */
157static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000158static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100159static int connection_in; /* Connection to server (input). */
160static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000161static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000162static int session_closed; /* In SSH2: login session closed. */
163static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000166int session_ident = -1;
167
Darren Tuckere32cf432010-01-08 16:51:40 +1100168int session_resumed = 0;
169
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000170/* Track escape per proto2 channel */
171struct escape_filter_ctx {
172 int escape_pending;
173 int escape_char;
174};
175
176/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000177struct channel_reply_ctx {
178 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000179 int id;
180 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000181};
182
Darren Tucker9f407c42008-06-13 04:50:27 +1000183/* Global request success/failure callbacks */
184struct global_confirm {
185 TAILQ_ENTRY(global_confirm) entry;
186 global_confirm_cb *cb;
187 void *ctx;
188 int ref_count;
189};
190TAILQ_HEAD(global_confirms, global_confirm);
191static struct global_confirms global_confirms =
192 TAILQ_HEAD_INITIALIZER(global_confirms);
193
Ben Lindstromf28f6342001-04-04 02:03:04 +0000194/*XXX*/
195extern Kex *xxx_kex;
196
Damien Miller0e220db2004-06-15 10:34:08 +1000197void ssh_process_session2_setup(int, int, int, Buffer *);
198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Restores stdin to blocking mode. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000205 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100206 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller95def091999-11-25 00:26:21 +1100210/* Puts stdin terminal in non-blocking mode. */
211
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000213enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000216 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Signal handler for the window change signal (SIGWINCH). This just sets a
221 * flag indicating that the window has changed.
222 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100223/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000224static void
Damien Miller95def091999-11-25 00:26:21 +1100225window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller95def091999-11-25 00:26:21 +1100227 received_window_change_signal = 1;
228 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Signal handler for signals that cause the program to terminate. These
233 * signals must be trapped to restore terminal modes.
234 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100235/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Miller95def091999-11-25 00:26:21 +1100237signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000239 received_signal = sig;
240 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Returns current time in seconds from Jan 1, 1970 with the maximum
245 * available resolution.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000249get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 struct timeval tv;
252 gettimeofday(&tv, NULL);
253 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Millere11e1ea2010-08-03 16:04:46 +1000256/*
257 * Sets control_persist_exit_time to the absolute time when the
258 * backgrounded control master should exit due to expiry of the
259 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
260 * control master process, or if there is no ControlPersist timeout.
261 */
262static void
263set_control_persist_exit_time(void)
264{
265 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000266 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000267 /* not using a ControlPersist timeout */
268 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000269 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000270 /* some client connections are still open */
271 if (control_persist_exit_time > 0)
272 debug2("%s: cancel scheduled exit", __func__);
273 control_persist_exit_time = 0;
274 } else if (control_persist_exit_time <= 0) {
275 /* a client connection has recently closed */
276 control_persist_exit_time = time(NULL) +
277 (time_t)options.control_persist_timeout;
278 debug2("%s: schedule exit in %d seconds", __func__,
279 options.control_persist_timeout);
280 }
281 /* else we are already counting down to the timeout */
282}
283
Damien Miller17e7ed02005-06-17 12:54:33 +1000284#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
285void
286client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000287 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000288{
289 char cmd[1024];
290 char line[512];
291 char xdisplay[512];
292 static char proto[512], data[512];
293 FILE *f;
294 int got_data = 0, generated = 0, do_unlink = 0, i;
295 char *xauthdir, *xauthfile;
296 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000297 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000298
299 xauthdir = xauthfile = NULL;
300 *_proto = proto;
301 *_data = data;
302 proto[0] = data[0] = '\0';
303
304 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
305 debug("No xauth program.");
306 } else {
307 if (display == NULL) {
308 debug("x11_get_proto: DISPLAY not set");
309 return;
310 }
311 /*
312 * Handle FamilyLocal case where $DISPLAY does
313 * not match an authorization entry. For this we
314 * just try "xauth list unix:displaynum.screennum".
315 * XXX: "localhost" match to determine FamilyLocal
316 * is not perfect.
317 */
318 if (strncmp(display, "localhost:", 10) == 0) {
319 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
320 display + 10);
321 display = xdisplay;
322 }
323 if (trusted == 0) {
324 xauthdir = xmalloc(MAXPATHLEN);
325 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100326 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000327 if (mkdtemp(xauthdir) != NULL) {
328 do_unlink = 1;
329 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
330 xauthdir);
331 snprintf(cmd, sizeof(cmd),
332 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000333 " untrusted timeout %u 2>" _PATH_DEVNULL,
334 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000335 debug2("x11_get_proto: %s", cmd);
336 if (system(cmd) == 0)
337 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000338 if (x11_refuse_time == 0) {
339 now = time(NULL) + 1;
340 if (UINT_MAX - timeout < now)
341 x11_refuse_time = UINT_MAX;
342 else
343 x11_refuse_time = now + timeout;
344 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000345 }
346 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000347
348 /*
349 * When in untrusted mode, we read the cookie only if it was
350 * successfully generated as an untrusted one in the step
351 * above.
352 */
353 if (trusted || generated) {
354 snprintf(cmd, sizeof(cmd),
355 "%s %s%s list %s 2>" _PATH_DEVNULL,
356 xauth_path,
357 generated ? "-f " : "" ,
358 generated ? xauthfile : "",
359 display);
360 debug2("x11_get_proto: %s", cmd);
361 f = popen(cmd, "r");
362 if (f && fgets(line, sizeof(line), f) &&
363 sscanf(line, "%*s %511s %511s", proto, data) == 2)
364 got_data = 1;
365 if (f)
366 pclose(f);
367 } else
368 error("Warning: untrusted X11 forwarding setup failed: "
369 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000370 }
371
372 if (do_unlink) {
373 unlink(xauthfile);
374 rmdir(xauthdir);
375 }
376 if (xauthdir)
377 xfree(xauthdir);
378 if (xauthfile)
379 xfree(xauthfile);
380
381 /*
382 * If we didn't get authentication data, just make up some
383 * data. The forwarding code will check the validity of the
384 * response anyway, and substitute this data. The X11
385 * server, however, will ignore this fake data and use
386 * whatever authentication mechanisms it was using otherwise
387 * for the local connection.
388 */
389 if (!got_data) {
390 u_int32_t rnd = 0;
391
392 logit("Warning: No xauth data; "
393 "using fake authentication data for X11 forwarding.");
394 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
395 for (i = 0; i < 16; i++) {
396 if (i % 4 == 0)
397 rnd = arc4random();
398 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
399 rnd & 0xff);
400 rnd >>= 8;
401 }
402 }
403}
404
Damien Miller5428f641999-11-25 11:54:57 +1100405/*
406 * This is called when the interactive is entered. This checks if there is
407 * an EOF coming on stdin. We must check this explicitly, as select() does
408 * not appear to wake up when redirecting from /dev/null.
409 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Ben Lindstrombba81212001-06-25 05:01:22 +0000411static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000412client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413{
Damien Miller95def091999-11-25 00:26:21 +1100414 int len;
415 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller5428f641999-11-25 11:54:57 +1100417 /*
418 * If standard input is to be "redirected from /dev/null", we simply
419 * mark that we have seen an EOF and send an EOF message to the
420 * server. Otherwise, we try to read a single character; it appears
421 * that for some files, such /dev/null, select() never wakes up for
422 * read for this descriptor, which means that we never get EOF. This
423 * way we will get the EOF if stdin comes from /dev/null or similar.
424 */
Damien Miller95def091999-11-25 00:26:21 +1100425 if (stdin_null_flag) {
426 /* Fake EOF on stdin. */
427 debug("Sending eof.");
428 stdin_eof = 1;
429 packet_start(SSH_CMSG_EOF);
430 packet_send();
431 } else {
Damien Miller95def091999-11-25 00:26:21 +1100432 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 /* Check for immediate EOF on stdin. */
435 len = read(fileno(stdin), buf, 1);
436 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000437 /*
438 * EOF. Record that we have seen it and send
439 * EOF to server.
440 */
Damien Miller95def091999-11-25 00:26:21 +1100441 debug("Sending eof.");
442 stdin_eof = 1;
443 packet_start(SSH_CMSG_EOF);
444 packet_send();
445 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100446 /*
447 * Got data. We must store the data in the buffer,
448 * and also process it as an escape character if
449 * appropriate.
450 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000451 if ((u_char) buf[0] == escape_char1)
452 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000453 else
Damien Miller95def091999-11-25 00:26:21 +1100454 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100455 }
Damien Miller95def091999-11-25 00:26:21 +1100456 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458}
459
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller5428f641999-11-25 11:54:57 +1100461/*
462 * Make packets from buffered stdin data, and buffer them for sending to the
463 * connection.
464 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Ben Lindstrombba81212001-06-25 05:01:22 +0000466static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000467client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000469 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 /* Send buffered stdin data to the server. */
472 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100473 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100474 len = buffer_len(&stdin_buffer);
475 /* Keep the packets at reasonable size. */
476 if (len > packet_get_maxsize())
477 len = packet_get_maxsize();
478 packet_start(SSH_CMSG_STDIN_DATA);
479 packet_put_string(buffer_ptr(&stdin_buffer), len);
480 packet_send();
481 buffer_consume(&stdin_buffer, len);
482 /* If we have a pending EOF, send it now. */
483 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
484 packet_start(SSH_CMSG_EOF);
485 packet_send();
486 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}
489
Damien Miller5428f641999-11-25 11:54:57 +1100490/*
491 * Checks if the client window has changed, and sends a packet about it to
492 * the server if so. The actual change is detected elsewhere (by a software
493 * interrupt on Unix); this just checks the flag and sends a message if
494 * appropriate.
495 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Ben Lindstrombba81212001-06-25 05:01:22 +0000497static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000498client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499{
Damien Miller1383bd82000-04-06 12:32:37 +1000500 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Miller1383bd82000-04-06 12:32:37 +1000502 if (! received_window_change_signal)
503 return;
504 /** XXX race */
505 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
Damien Millerd3444942000-09-30 14:20:03 +1100507 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000508
509 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000510 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000511 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000512 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
513 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000514 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100515 packet_put_int((u_int)ws.ws_row);
516 packet_put_int((u_int)ws.ws_col);
517 packet_put_int((u_int)ws.ws_xpixel);
518 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000519 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521}
522
Damien Miller509b0102003-12-17 16:33:10 +1100523static void
524client_global_request_reply(int type, u_int32_t seq, void *ctxt)
525{
Darren Tucker9f407c42008-06-13 04:50:27 +1000526 struct global_confirm *gc;
527
528 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
529 return;
530 if (gc->cb != NULL)
531 gc->cb(type, seq, gc->ctx);
532 if (--gc->ref_count <= 0) {
533 TAILQ_REMOVE(&global_confirms, gc, entry);
534 bzero(gc, sizeof(*gc));
535 xfree(gc);
536 }
537
Darren Tuckerf7288d72009-06-21 18:12:20 +1000538 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100539}
540
541static void
542server_alive_check(void)
543{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000544 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100545 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000546 cleanup_exit(255);
547 }
Damien Miller509b0102003-12-17 16:33:10 +1100548 packet_start(SSH2_MSG_GLOBAL_REQUEST);
549 packet_put_cstring("keepalive@openssh.com");
550 packet_put_char(1); /* boolean: want reply */
551 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000552 /* Insert an empty placeholder to maintain ordering */
553 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100554}
555
Damien Miller5428f641999-11-25 11:54:57 +1100556/*
557 * Waits until the client can do something (some data becomes available on
558 * one of the file descriptors).
559 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000560static void
Damien Miller5e953212001-01-30 09:14:00 +1100561client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000562 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563{
Damien Miller509b0102003-12-17 16:33:10 +1100564 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000565 int timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100566 int ret;
567
Damien Miller5e953212001-01-30 09:14:00 +1100568 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000569 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller1383bd82000-04-06 12:32:37 +1000571 if (!compat20) {
572 /* Read from the connection, unless our buffers are full. */
573 if (buffer_len(&stdout_buffer) < buffer_high &&
574 buffer_len(&stderr_buffer) < buffer_high &&
575 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100576 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000577 /*
578 * Read from stdin, unless we have seen EOF or have very much
579 * buffered data to send to the server.
580 */
581 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100582 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000583
584 /* Select stdout/stderr if have data in buffer. */
585 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100586 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000587 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100588 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000589 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000590 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000591 if (session_closed && !channel_still_open() &&
592 !packet_have_data_to_write()) {
593 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100594 memset(*readsetp, 0, *nallocp);
595 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000596 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000597 } else {
598 FD_SET(connection_in, *readsetp);
599 }
Damien Miller1383bd82000-04-06 12:32:37 +1000600 }
601
Damien Miller95def091999-11-25 00:26:21 +1100602 /* Select server connection if have data to write to the server. */
603 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100604 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100605
Damien Miller5428f641999-11-25 11:54:57 +1100606 /*
607 * Wait for something to happen. This will suspend the process until
608 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000609 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100610 */
Damien Miller95def091999-11-25 00:26:21 +1100611
Damien Millere11e1ea2010-08-03 16:04:46 +1000612 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
613 if (options.server_alive_interval > 0 && compat20)
614 timeout_secs = options.server_alive_interval;
615 set_control_persist_exit_time();
616 if (control_persist_exit_time > 0) {
617 timeout_secs = MIN(timeout_secs,
618 control_persist_exit_time - time(NULL));
619 if (timeout_secs < 0)
620 timeout_secs = 0;
621 }
622 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100623 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000624 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000625 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100626 tv.tv_usec = 0;
627 tvp = &tv;
628 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000629
Damien Miller509b0102003-12-17 16:33:10 +1100630 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
631 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100632 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000633
634 /*
635 * We have to clear the select masks, because we return.
636 * We have to return, because the mainloop checks for the flags
637 * set by the signal handlers.
638 */
Damien Miller79faeff2001-11-12 11:06:32 +1100639 memset(*readsetp, 0, *nallocp);
640 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000641
Damien Miller95def091999-11-25 00:26:21 +1100642 if (errno == EINTR)
643 return;
644 /* Note: we might still have data in the buffers. */
645 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
646 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100647 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100648 } else if (ret == 0)
649 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650}
651
Ben Lindstrombba81212001-06-25 05:01:22 +0000652static void
Damien Millerad833b32000-08-23 10:46:23 +1000653client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654{
Damien Miller95def091999-11-25 00:26:21 +1100655 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000656 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000657 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
658 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000659 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000660 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
661 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller21771e22011-05-15 08:45:50 +1000663 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller5428f641999-11-25 11:54:57 +1100665 /*
666 * Free (and clear) the buffer to reduce the amount of data that gets
667 * written to swap.
668 */
Damien Millerad833b32000-08-23 10:46:23 +1000669 buffer_free(bin);
670 buffer_free(bout);
671 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller95def091999-11-25 00:26:21 +1100673 /* Send the suspend signal to the program itself. */
674 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675
Darren Tucker5d78de62004-11-05 20:35:44 +1100676 /* Reset window sizes in case they have changed */
677 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
Damien Miller95def091999-11-25 00:26:21 +1100679 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000680 buffer_init(bin);
681 buffer_init(bout);
682 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683
Damien Miller21771e22011-05-15 08:45:50 +1000684 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685}
686
Ben Lindstrombba81212001-06-25 05:01:22 +0000687static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100688client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000690 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000691 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692
Damien Miller5428f641999-11-25 11:54:57 +1100693 /*
694 * Read input from the server, and add any such data to the buffer of
695 * the packet subsystem.
696 */
Damien Miller95def091999-11-25 00:26:21 +1100697 if (FD_ISSET(connection_in, readset)) {
698 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000699 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
700 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000701 /*
702 * Received EOF. The remote host has closed the
703 * connection.
704 */
705 snprintf(buf, sizeof buf,
706 "Connection to %.300s closed by remote host.\r\n",
707 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 quit_pending = 1;
710 return;
Damien Miller95def091999-11-25 00:26:21 +1100711 }
Damien Miller5428f641999-11-25 11:54:57 +1100712 /*
713 * There is a kernel bug on Solaris that causes select to
714 * sometimes wake up even though there is no data available.
715 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000716 if (len < 0 &&
717 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100718 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller95def091999-11-25 00:26:21 +1100720 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000721 /*
722 * An error has encountered. Perhaps there is a
723 * network problem.
724 */
725 snprintf(buf, sizeof buf,
726 "Read from remote host %.300s: %.100s\r\n",
727 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100728 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100729 quit_pending = 1;
730 return;
731 }
732 packet_process_incoming(buf, len);
733 }
Damien Miller1383bd82000-04-06 12:32:37 +1000734}
735
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000736static void
Damien Miller5771ed72008-05-19 15:35:33 +1000737client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000738{
Damien Miller5771ed72008-05-19 15:35:33 +1000739 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
740 char errmsg[256];
741 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000742
Damien Miller555f3b82011-05-15 08:48:05 +1000743 /*
744 * If a TTY was explicitly requested, then a failure to allocate
745 * one is fatal.
746 */
747 if (cr->action == CONFIRM_TTY &&
748 (options.request_tty == REQUEST_TTY_FORCE ||
749 options.request_tty == REQUEST_TTY_YES))
750 cr->action = CONFIRM_CLOSE;
751
Damien Miller5771ed72008-05-19 15:35:33 +1000752 /* XXX supress on mux _client_ quietmode */
753 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100754 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000755
Damien Miller5771ed72008-05-19 15:35:33 +1000756 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
757 debug2("%s request accepted on channel %d",
758 cr->request_type, c->self);
759 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
760 if (tochan) {
761 snprintf(errmsg, sizeof(errmsg),
762 "%s request failed\r\n", cr->request_type);
763 } else {
764 snprintf(errmsg, sizeof(errmsg),
765 "%s request failed on channel %d",
766 cr->request_type, c->self);
767 }
768 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000769 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000770 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000771 /*
772 * If error occurred on mux client, append to
773 * their stderr.
774 */
775 if (tochan) {
776 buffer_append(&c->extended, errmsg,
777 strlen(errmsg));
778 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000779 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000780 if (cr->action == CONFIRM_TTY) {
781 /*
782 * If a TTY allocation error occurred, then arrange
783 * for the correct TTY to leave raw mode.
784 */
785 if (c->self == session_ident)
786 leave_raw_mode(0);
787 else
788 mux_tty_alloc_failed(c);
789 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000790 chan_read_failed(c);
791 chan_write_failed(c);
792 }
Damien Miller0e220db2004-06-15 10:34:08 +1000793 }
Damien Miller5771ed72008-05-19 15:35:33 +1000794 xfree(cr);
795}
Damien Miller0e220db2004-06-15 10:34:08 +1000796
Damien Miller5771ed72008-05-19 15:35:33 +1000797static void
798client_abandon_status_confirm(Channel *c, void *ctx)
799{
800 xfree(ctx);
801}
802
Damien Miller6d7b4372011-06-23 08:31:57 +1000803void
Damien Miller555f3b82011-05-15 08:48:05 +1000804client_expect_confirm(int id, const char *request,
805 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000806{
807 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
808
809 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000810 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000811
812 channel_register_status_confirm(id, client_status_confirm,
813 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000814}
815
Darren Tucker9f407c42008-06-13 04:50:27 +1000816void
817client_register_global_confirm(global_confirm_cb *cb, void *ctx)
818{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000819 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000820
821 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000822 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
823 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
824 if (++last_gc->ref_count >= INT_MAX)
825 fatal("%s: last_gc->ref_count = %d",
826 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000827 return;
828 }
829
830 gc = xmalloc(sizeof(*gc));
831 gc->cb = cb;
832 gc->ctx = ctx;
833 gc->ref_count = 1;
834 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
835}
836
Damien Miller0e220db2004-06-15 10:34:08 +1000837static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000838process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000839{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000840 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100841 char *s, *cmd, *cancel_host;
Damien Millerff773642011-09-22 21:39:48 +1000842 int delete = 0, local = 0, remote = 0, dynamic = 0;
843 int cancel_port, ok;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100844 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000845
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100846 bzero(&fwd, sizeof(fwd));
847 fwd.listen_host = fwd.connect_host = NULL;
848
Damien Miller21771e22011-05-15 08:45:50 +1000849 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000850 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000851 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000852 if (s == NULL)
853 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100854 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000855 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000856 if (*s == '-')
857 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000858 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000859 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000860
Darren Tucker1973c882004-05-24 10:34:36 +1000861 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000862 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000863 logit(" -L[bind_address:]port:host:hostport "
864 "Request local forward");
865 logit(" -R[bind_address:]port:host:hostport "
866 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100867 logit(" -D[bind_address:]port "
868 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000869 logit(" -KL[bind_address:]port "
870 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000871 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000872 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000873 logit(" -KD[bind_address:]port "
874 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100875 if (!options.permit_local_command)
876 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000877 logit(" !args "
878 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100879 goto out;
880 }
881
882 if (*s == '!' && options.permit_local_command) {
883 s++;
884 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000885 goto out;
886 }
887
888 if (*s == 'K') {
889 delete = 1;
890 s++;
891 }
Damien Miller0164cb82008-11-05 16:30:31 +1100892 if (*s == 'L')
893 local = 1;
894 else if (*s == 'R')
895 remote = 1;
896 else if (*s == 'D')
897 dynamic = 1;
898 else {
Damien Miller996acd22003-04-09 20:59:48 +1000899 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000900 goto out;
901 }
Damien Miller0164cb82008-11-05 16:30:31 +1100902
Damien Millerff773642011-09-22 21:39:48 +1000903 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000904 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000905 goto out;
906 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000907
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100908 while (isspace(*++s))
909 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000910
Damien Millere1537f92010-01-26 13:26:22 +1100911 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000912 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100913 cancel_port = 0;
914 cancel_host = hpdelim(&s); /* may be NULL */
915 if (s != NULL) {
916 cancel_port = a2port(s);
917 cancel_host = cleanhostname(cancel_host);
918 } else {
919 cancel_port = a2port(cancel_host);
920 cancel_host = NULL;
921 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100922 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100923 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000924 goto out;
925 }
Damien Millerff773642011-09-22 21:39:48 +1000926 if (remote)
927 ok = channel_request_rforward_cancel(cancel_host,
928 cancel_port) == 0;
929 else if (dynamic)
930 ok = channel_cancel_lport_listener(cancel_host,
931 cancel_port, 0, options.gateway_ports) > 0;
932 else
933 ok = channel_cancel_lport_listener(cancel_host,
934 cancel_port, CHANNEL_CANCEL_PORT_STATIC,
935 options.gateway_ports) > 0;
936 if (!ok) {
937 logit("Unkown port forwarding.");
938 goto out;
939 }
940 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000941 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100942 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000943 logit("Bad forwarding specification.");
944 goto out;
945 }
Damien Miller0164cb82008-11-05 16:30:31 +1100946 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100947 if (channel_setup_local_fwd_listener(fwd.listen_host,
948 fwd.listen_port, fwd.connect_host,
949 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000950 logit("Port forwarding failed.");
951 goto out;
952 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100953 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000954 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100955 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000956 fwd.connect_port) < 0) {
957 logit("Port forwarding failed.");
958 goto out;
959 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100960 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000961 logit("Forwarding port.");
962 }
963
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000964out:
965 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000966 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000967 if (cmd)
968 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100969 if (fwd.listen_host != NULL)
970 xfree(fwd.listen_host);
971 if (fwd.connect_host != NULL)
972 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000973}
974
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000975/*
976 * Process the characters one by one, call with c==NULL for proto1 case.
977 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000978static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000979process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
980 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000981{
982 char string[1024];
983 pid_t pid;
984 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000985 u_int i;
986 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000987 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000988 int *escape_pendingp, escape_char;
989 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000990
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000991 if (c == NULL) {
992 escape_pendingp = &escape_pending1;
993 escape_char = escape_char1;
994 } else {
995 if (c->filter_ctx == NULL)
996 return 0;
997 efc = (struct escape_filter_ctx *)c->filter_ctx;
998 escape_pendingp = &efc->escape_pending;
999 escape_char = efc->escape_char;
1000 }
1001
Damien Millereccb9de2005-06-17 12:59:34 +10001002 if (len <= 0)
1003 return (0);
1004
1005 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001006 /* Get one character at a time. */
1007 ch = buf[i];
1008
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001009 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001010 /* We have previously seen an escape character. */
1011 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001012 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001013
1014 /* Process the escaped character. */
1015 switch (ch) {
1016 case '.':
1017 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001018 snprintf(string, sizeof string, "%c.\r\n",
1019 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001020 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001021
Damien Millere1537f92010-01-26 13:26:22 +11001022 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001023 chan_read_failed(c);
1024 chan_write_failed(c);
1025 return 0;
1026 } else
1027 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001028 return -1;
1029
1030 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001031 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001032 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001033 noescape:
1034 snprintf(string, sizeof string,
1035 "%c%c escape not available to "
1036 "multiplexed sessions\r\n",
1037 escape_char, ch);
1038 buffer_append(berr, string,
1039 strlen(string));
1040 continue;
1041 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001042 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001043 snprintf(string, sizeof string,
1044 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001045 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001046
1047 /* Restore terminal modes and suspend. */
1048 client_suspend_self(bin, bout, berr);
1049
1050 /* We have been continued. */
1051 continue;
1052
Damien Miller54c45982003-05-15 10:20:13 +10001053 case 'B':
1054 if (compat20) {
1055 snprintf(string, sizeof string,
1056 "%cB\r\n", escape_char);
1057 buffer_append(berr, string,
1058 strlen(string));
1059 channel_request_start(session_ident,
1060 "break", 0);
1061 packet_put_int(1000);
1062 packet_send();
1063 }
1064 continue;
1065
Ben Lindstromf28f6342001-04-04 02:03:04 +00001066 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001067 if (compat20) {
1068 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001069 logit("Server does not "
1070 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001071 else
1072 need_rekeying = 1;
1073 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001074 continue;
1075
Damien Millerad833b32000-08-23 10:46:23 +10001076 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001077 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001078 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001079 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001080 * Detach the program (continue to serve
1081 * connections, but put in background and no
1082 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001083 */
Damien Miller96507ef2001-11-12 10:52:25 +11001084 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001085 leave_raw_mode(
1086 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001087
1088 /* Stop listening for new connections. */
1089 channel_stop_listening();
1090
1091 snprintf(string, sizeof string,
1092 "%c& [backgrounded]\n", escape_char);
1093 buffer_append(berr, string, strlen(string));
1094
1095 /* Fork into background. */
1096 pid = fork();
1097 if (pid < 0) {
1098 error("fork: %.100s", strerror(errno));
1099 continue;
1100 }
1101 if (pid != 0) { /* This is the parent. */
1102 /* The parent just exits. */
1103 exit(0);
1104 }
1105 /* The child continues serving connections. */
1106 if (compat20) {
1107 buffer_append(bin, "\004", 1);
1108 /* fake EOF on stdin */
1109 return -1;
1110 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001111 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001112 * Sending SSH_CMSG_EOF alone does not
1113 * always appear to be enough. So we
1114 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001115 */
1116 packet_start(SSH_CMSG_STDIN_DATA);
1117 packet_put_string("\004", 1);
1118 packet_send();
1119 /* Close stdin. */
1120 stdin_eof = 1;
1121 if (buffer_len(bin) == 0) {
1122 packet_start(SSH_CMSG_EOF);
1123 packet_send();
1124 }
1125 }
Damien Miller96507ef2001-11-12 10:52:25 +11001126 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001127
1128 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001129 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001130 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001131"%c?\r\n\
1132Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001133 %c. - terminate session\r\n\
1134 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001135 %cR - Request rekey (SSH protocol 2 only)\r\n\
1136 %c# - list forwarded connections\r\n\
1137 %c? - this message\r\n\
1138 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001139(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001140 escape_char, escape_char,
1141 escape_char, escape_char,
1142 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001143 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001144 } else {
1145 snprintf(string, sizeof string,
1146"%c?\r\n\
1147Supported escape sequences:\r\n\
1148 %c. - terminate connection (and any multiplexed sessions)\r\n\
1149 %cB - send a BREAK to the remote system\r\n\
1150 %cC - open a command line\r\n\
1151 %cR - Request rekey (SSH protocol 2 only)\r\n\
1152 %c^Z - suspend ssh\r\n\
1153 %c# - list forwarded connections\r\n\
1154 %c& - background ssh (when waiting for connections to terminate)\r\n\
1155 %c? - this message\r\n\
1156 %c%c - send the escape character by typing it twice\r\n\
1157(Note that escapes are only recognized immediately after newline.)\r\n",
1158 escape_char, escape_char,
1159 escape_char, escape_char,
1160 escape_char, escape_char,
1161 escape_char, escape_char,
1162 escape_char, escape_char,
1163 escape_char);
1164 }
Damien Millerad833b32000-08-23 10:46:23 +10001165 buffer_append(berr, string, strlen(string));
1166 continue;
1167
1168 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001169 snprintf(string, sizeof string, "%c#\r\n",
1170 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001171 buffer_append(berr, string, strlen(string));
1172 s = channel_open_message();
1173 buffer_append(berr, s, strlen(s));
1174 xfree(s);
1175 continue;
1176
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001177 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001178 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001179 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001180 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001181 continue;
1182
Damien Millerad833b32000-08-23 10:46:23 +10001183 default:
1184 if (ch != escape_char) {
1185 buffer_put_char(bin, escape_char);
1186 bytes++;
1187 }
1188 /* Escaped characters fall through here */
1189 break;
1190 }
1191 } else {
1192 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001193 * The previous character was not an escape char.
1194 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001195 */
1196 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001197 /*
1198 * It is. Set the flag and continue to
1199 * next character.
1200 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001201 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001202 continue;
1203 }
1204 }
1205
1206 /*
1207 * Normal character. Record whether it was a newline,
1208 * and append it to the buffer.
1209 */
1210 last_was_cr = (ch == '\r' || ch == '\n');
1211 buffer_put_char(bin, ch);
1212 bytes++;
1213 }
1214 return bytes;
1215}
1216
Ben Lindstrombba81212001-06-25 05:01:22 +00001217static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001218client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001219{
Damien Miller166fca82000-04-20 07:42:21 +10001220 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001221 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001222
Damien Miller95def091999-11-25 00:26:21 +11001223 /* Read input from stdin. */
1224 if (FD_ISSET(fileno(stdin), readset)) {
1225 /* Read as much as possible. */
1226 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001227 if (len < 0 &&
1228 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001229 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001230 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001231 /*
1232 * Received EOF or error. They are treated
1233 * similarly, except that an error message is printed
1234 * if it was an error condition.
1235 */
Damien Miller95def091999-11-25 00:26:21 +11001236 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001237 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1238 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001239 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001240 }
1241 /* Mark that we have seen EOF. */
1242 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001243 /*
1244 * Send an EOF message to the server unless there is
1245 * data in the buffer. If there is data in the
1246 * buffer, no message will be sent now. Code
1247 * elsewhere will send the EOF when the buffer
1248 * becomes empty if stdin_eof is set.
1249 */
Damien Miller95def091999-11-25 00:26:21 +11001250 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251 packet_start(SSH_CMSG_EOF);
1252 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001253 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001254 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001255 /*
1256 * Normal successful read, and no escape character.
1257 * Just append the data to buffer.
1258 */
Damien Miller95def091999-11-25 00:26:21 +11001259 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001260 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001261 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001262 * Normal, successful read. But we have an escape
1263 * character and have to process the characters one
1264 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001265 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001266 if (process_escapes(NULL, &stdin_buffer,
1267 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001268 return;
Damien Miller95def091999-11-25 00:26:21 +11001269 }
1270 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271}
1272
Ben Lindstrombba81212001-06-25 05:01:22 +00001273static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001274client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275{
Damien Miller95def091999-11-25 00:26:21 +11001276 int len;
1277 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278
Damien Miller95def091999-11-25 00:26:21 +11001279 /* Write buffered output to stdout. */
1280 if (FD_ISSET(fileno(stdout), writeset)) {
1281 /* Write as much data as possible. */
1282 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001283 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001284 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001285 if (errno == EINTR || errno == EAGAIN ||
1286 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001287 len = 0;
1288 else {
Damien Miller5428f641999-11-25 11:54:57 +11001289 /*
1290 * An error or EOF was encountered. Put an
1291 * error message to stderr buffer.
1292 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001293 snprintf(buf, sizeof buf,
1294 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001295 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001296 quit_pending = 1;
1297 return;
1298 }
1299 }
1300 /* Consume printed data from the buffer. */
1301 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302 }
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Write buffered output to stderr. */
1304 if (FD_ISSET(fileno(stderr), writeset)) {
1305 /* Write as much data as possible. */
1306 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001307 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001308 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001309 if (errno == EINTR || errno == EAGAIN ||
1310 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001311 len = 0;
1312 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001313 /*
1314 * EOF or error, but can't even print
1315 * error message.
1316 */
Damien Miller95def091999-11-25 00:26:21 +11001317 quit_pending = 1;
1318 return;
1319 }
1320 }
1321 /* Consume printed characters from the buffer. */
1322 buffer_consume(&stderr_buffer, len);
1323 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324}
1325
Damien Miller5428f641999-11-25 11:54:57 +11001326/*
Damien Millerb38eff82000-04-01 11:09:21 +10001327 * Get packets from the connection input buffer, and process them as long as
1328 * there are packets available.
1329 *
1330 * Any unknown packets received during the actual
1331 * session cause the session to terminate. This is
1332 * intended to make debugging easier since no
1333 * confirmations are sent. Any compatible protocol
1334 * extensions must be negotiated during the
1335 * preparatory phase.
1336 */
1337
Ben Lindstrombba81212001-06-25 05:01:22 +00001338static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001339client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001340{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001341 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1342 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001343}
1344
Damien Millerad833b32000-08-23 10:46:23 +10001345/* scan buf[] for '~' before sending data to the peer */
1346
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001347/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1348void *
1349client_new_escape_filter_ctx(int escape_char)
1350{
1351 struct escape_filter_ctx *ret;
1352
1353 ret = xmalloc(sizeof(*ret));
1354 ret->escape_pending = 0;
1355 ret->escape_char = escape_char;
1356 return (void *)ret;
1357}
1358
Darren Tucker84c56f52008-06-13 04:55:46 +10001359/* Free the escape filter context on channel free */
1360void
1361client_filter_cleanup(int cid, void *ctx)
1362{
1363 xfree(ctx);
1364}
1365
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001366int
1367client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001368{
Damien Miller5771ed72008-05-19 15:35:33 +10001369 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1370 return 0;
1371
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001372 return process_escapes(c, &c->input, &c->output, &c->extended,
1373 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001374}
1375
Ben Lindstrombba81212001-06-25 05:01:22 +00001376static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001377client_channel_closed(int id, void *arg)
1378{
Damien Miller3ec27592001-10-12 11:35:04 +10001379 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001380 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001381 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001382}
1383
Damien Millerb38eff82000-04-01 11:09:21 +10001384/*
Damien Miller5428f641999-11-25 11:54:57 +11001385 * Implements the interactive session with the server. This is called after
1386 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001387 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1388 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001389 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller4af51302000-04-16 11:18:38 +10001391int
Damien Millerad833b32000-08-23 10:46:23 +10001392client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393{
Damien Miller5e953212001-01-30 09:14:00 +11001394 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001395 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001396 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001397 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001398 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001399 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Damien Miller95def091999-11-25 00:26:21 +11001403 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Damien Miller95def091999-11-25 00:26:21 +11001405 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001406 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001407 last_was_cr = 1;
1408 exit_status = -1;
1409 stdin_eof = 0;
1410 buffer_high = 64 * 1024;
1411 connection_in = packet_get_connection_in();
1412 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001413 max_fd = MAX(connection_in, connection_out);
1414
1415 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001416 /* enable nonblocking unless tty */
1417 if (!isatty(fileno(stdin)))
1418 set_nonblock(fileno(stdin));
1419 if (!isatty(fileno(stdout)))
1420 set_nonblock(fileno(stdout));
1421 if (!isatty(fileno(stderr)))
1422 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001423 max_fd = MAX(max_fd, fileno(stdin));
1424 max_fd = MAX(max_fd, fileno(stdout));
1425 max_fd = MAX(max_fd, fileno(stderr));
1426 }
Damien Miller95def091999-11-25 00:26:21 +11001427 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001428 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Initialize buffers. */
1431 buffer_init(&stdin_buffer);
1432 buffer_init(&stdout_buffer);
1433 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Damien Millerb38eff82000-04-01 11:09:21 +10001435 client_init_dispatch();
1436
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001437 /*
1438 * Set signal handlers, (e.g. to restore non-blocking mode)
1439 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1440 */
Darren Tucker07336da2004-11-05 20:02:16 +11001441 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1442 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001443 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1444 signal(SIGINT, signal_handler);
1445 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1446 signal(SIGQUIT, signal_handler);
1447 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1448 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001449 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450
Damien Miller95def091999-11-25 00:26:21 +11001451 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001452 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Ben Lindstrom5b828322001-02-09 01:34:36 +00001454 if (compat20) {
1455 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001456 if (session_ident != -1) {
1457 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1458 channel_register_filter(session_ident,
1459 client_simple_escape_filter, NULL,
1460 client_filter_cleanup,
1461 client_new_escape_filter_ctx(
1462 escape_char_arg));
1463 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001464 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001465 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001466 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001467 } else {
1468 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001469 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001470 }
Damien Millerad833b32000-08-23 10:46:23 +10001471
Damien Miller95def091999-11-25 00:26:21 +11001472 /* Main loop of the client for the interactive session mode. */
1473 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474
Damien Miller5428f641999-11-25 11:54:57 +11001475 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001476 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001478 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001479 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001480
1481 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001482
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001483 if (rekeying) {
1484 debug("rekeying in progress");
1485 } else {
1486 /*
1487 * Make packets of buffered stdin data, and buffer
1488 * them for sending to the server.
1489 */
1490 if (!compat20)
1491 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001493 /*
1494 * Make packets from buffered channel data, and
1495 * enqueue them for sending to the server.
1496 */
1497 if (packet_not_very_much_data_to_write())
1498 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001500 /*
1501 * Check if the window size has changed, and buffer a
1502 * message about it to the server if so.
1503 */
1504 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001506 if (quit_pending)
1507 break;
1508 }
Damien Miller5428f641999-11-25 11:54:57 +11001509 /*
1510 * Wait until we have something to do (something becomes
1511 * available on one of the descriptors).
1512 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001513 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001514 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001515 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Damien Miller95def091999-11-25 00:26:21 +11001517 if (quit_pending)
1518 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001520 /* Do channel operations unless rekeying in progress. */
1521 if (!rekeying) {
1522 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001523 if (need_rekeying || packet_need_rekeying()) {
1524 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001525 xxx_kex->done = 0;
1526 kex_send_kexinit(xxx_kex);
1527 need_rekeying = 0;
1528 }
1529 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Damien Miller1383bd82000-04-06 12:32:37 +10001531 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001532 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533
Damien Miller1383bd82000-04-06 12:32:37 +10001534 if (quit_pending)
1535 break;
1536
1537 if (!compat20) {
1538 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001539 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001540 /*
1541 * Process output to stdout and stderr. Output to
1542 * the connection is processed elsewhere (above).
1543 */
Damien Miller5e953212001-01-30 09:14:00 +11001544 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001545 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546
Darren Tuckere32cf432010-01-08 16:51:40 +11001547 if (session_resumed) {
1548 connection_in = packet_get_connection_in();
1549 connection_out = packet_get_connection_out();
1550 max_fd = MAX(max_fd, connection_out);
1551 max_fd = MAX(max_fd, connection_in);
1552 session_resumed = 0;
1553 }
1554
Darren Tucker4d5cd332008-06-13 04:51:14 +10001555 /*
1556 * Send as much buffered packet data as possible to the
1557 * sender.
1558 */
Damien Miller5e953212001-01-30 09:14:00 +11001559 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001560 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001561
1562 /*
1563 * If we are a backgrounded control master, and the
1564 * timeout has expired without any active client
1565 * connections, then quit.
1566 */
1567 if (control_persist_exit_time > 0) {
1568 if (time(NULL) >= control_persist_exit_time) {
1569 debug("ControlPersist timeout expired");
1570 break;
1571 }
1572 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573 }
Damien Miller5e953212001-01-30 09:14:00 +11001574 if (readset)
1575 xfree(readset);
1576 if (writeset)
1577 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578
Damien Miller95def091999-11-25 00:26:21 +11001579 /* Terminate the session. */
1580
1581 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001582 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001583
Darren Tuckerde0c0252009-07-06 07:17:35 +10001584 if (compat20) {
1585 packet_start(SSH2_MSG_DISCONNECT);
1586 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1587 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001588 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001589 packet_send();
1590 packet_write_wait();
1591 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001592
Ben Lindstrom601e4362001-06-21 03:19:23 +00001593 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001594
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001595 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001596 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001597
1598 /* restore blocking io */
1599 if (!isatty(fileno(stdin)))
1600 unset_nonblock(fileno(stdin));
1601 if (!isatty(fileno(stdout)))
1602 unset_nonblock(fileno(stdout));
1603 if (!isatty(fileno(stderr)))
1604 unset_nonblock(fileno(stderr));
1605
Damien Millerd6965512003-12-17 16:31:53 +11001606 /*
1607 * If there was no shell or command requested, there will be no remote
1608 * exit status to be returned. In that case, clear error code if the
1609 * connection was deliberately terminated at this end.
1610 */
1611 if (no_shell_flag && received_signal == SIGTERM) {
1612 received_signal = 0;
1613 exit_status = 0;
1614 }
1615
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001616 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001617 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001618
1619 /*
1620 * In interactive mode (with pseudo tty) display a message indicating
1621 * that the connection has been closed.
1622 */
1623 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001624 snprintf(buf, sizeof buf,
1625 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001626 buffer_append(&stderr_buffer, buf, strlen(buf));
1627 }
1628
Damien Miller95def091999-11-25 00:26:21 +11001629 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001630 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001631 len = atomicio(vwrite, fileno(stdout),
1632 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001633 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001634 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001635 else
1636 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001637 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638
Damien Miller95def091999-11-25 00:26:21 +11001639 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001640 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001641 len = atomicio(vwrite, fileno(stderr),
1642 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001643 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001644 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001645 else
1646 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001647 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648
Damien Miller95def091999-11-25 00:26:21 +11001649 /* Clear and free any buffers. */
1650 memset(buf, 0, sizeof(buf));
1651 buffer_free(&stdin_buffer);
1652 buffer_free(&stdout_buffer);
1653 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001654
Damien Miller95def091999-11-25 00:26:21 +11001655 /* Report bytes transferred, and transfer rates. */
1656 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001657 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1658 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1659 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001660 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001661 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001662 verbose("Bytes per second: sent %.1f, received %.1f",
1663 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001664 /* Return the exit status of the program. */
1665 debug("Exit status %d", exit_status);
1666 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001667}
Damien Millerb38eff82000-04-01 11:09:21 +10001668
1669/*********/
1670
Ben Lindstrombba81212001-06-25 05:01:22 +00001671static void
Damien Miller630d6f42002-01-22 23:17:30 +11001672client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001673{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001674 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001675 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001676 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001677 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001678 memset(data, 0, data_len);
1679 xfree(data);
1680}
Ben Lindstrombba81212001-06-25 05:01:22 +00001681static void
Damien Miller630d6f42002-01-22 23:17:30 +11001682client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001683{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001684 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001685 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001686 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001687 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001688 memset(data, 0, data_len);
1689 xfree(data);
1690}
Ben Lindstrombba81212001-06-25 05:01:22 +00001691static void
Damien Miller630d6f42002-01-22 23:17:30 +11001692client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001693{
Damien Millerb38eff82000-04-01 11:09:21 +10001694 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001695 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001696 /* Acknowledge the exit. */
1697 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1698 packet_send();
1699 /*
1700 * Must wait for packet to be sent since we are
1701 * exiting the loop.
1702 */
1703 packet_write_wait();
1704 /* Flag that we want to exit. */
1705 quit_pending = 1;
1706}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001707static void
1708client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1709{
1710 Channel *c = NULL;
1711 int remote_id, sock;
1712
1713 /* Read the remote channel number from the message. */
1714 remote_id = packet_get_int();
1715 packet_check_eom();
1716
1717 /*
1718 * Get a connection to the local authentication agent (this may again
1719 * get forwarded).
1720 */
1721 sock = ssh_get_authentication_socket();
1722
1723 /*
1724 * If we could not connect the agent, send an error message back to
1725 * the server. This should never happen unless the agent dies,
1726 * because authentication forwarding is only enabled if we have an
1727 * agent.
1728 */
1729 if (sock >= 0) {
1730 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1731 -1, 0, 0, 0, "authentication agent connection", 1);
1732 c->remote_id = remote_id;
1733 c->force_drain = 1;
1734 }
1735 if (c == NULL) {
1736 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1737 packet_put_int(remote_id);
1738 } else {
1739 /* Send a confirmation to the remote host. */
1740 debug("Forwarding authentication connection.");
1741 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1742 packet_put_int(remote_id);
1743 packet_put_int(c->self);
1744 }
1745 packet_send();
1746}
Damien Millerb38eff82000-04-01 11:09:21 +10001747
Ben Lindstrombba81212001-06-25 05:01:22 +00001748static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001749client_request_forwarded_tcpip(const char *request_type, int rchan)
1750{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001751 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001752 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001753 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001754
1755 /* Get rest of the packet */
1756 listen_address = packet_get_string(NULL);
1757 listen_port = packet_get_int();
1758 originator_address = packet_get_string(NULL);
1759 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001760 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001761
Damien Millerbd740252008-05-19 15:37:09 +10001762 debug("client_request_forwarded_tcpip: listen %s port %d, "
1763 "originator %s port %d", listen_address, listen_port,
1764 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001765
Damien Millerbd740252008-05-19 15:37:09 +10001766 c = channel_connect_by_listen_address(listen_port,
1767 "forwarded-tcpip", originator_address);
1768
Damien Miller0bc1bd82000-11-13 22:57:25 +11001769 xfree(originator_address);
1770 xfree(listen_address);
1771 return c;
1772}
1773
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001774static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775client_request_x11(const char *request_type, int rchan)
1776{
1777 Channel *c = NULL;
1778 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001779 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001780 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001781
1782 if (!options.forward_x11) {
1783 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001784 error("Warning: this is probably a break-in attempt by a "
1785 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001786 return NULL;
1787 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001788 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1789 verbose("Rejected X11 connection after ForwardX11Timeout "
1790 "expired");
1791 return NULL;
1792 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001793 originator = packet_get_string(NULL);
1794 if (datafellows & SSH_BUG_X11FWD) {
1795 debug2("buggy server: x11 request w/o originator_port");
1796 originator_port = 0;
1797 } else {
1798 originator_port = packet_get_int();
1799 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001800 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001801 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001802 debug("client_request_x11: request from %s %d", originator,
1803 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001804 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001805 sock = x11_connect_display();
1806 if (sock < 0)
1807 return NULL;
1808 c = channel_new("x11",
1809 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001810 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001811 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001812 return c;
1813}
1814
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001815static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001816client_request_agent(const char *request_type, int rchan)
1817{
1818 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001819 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001820
1821 if (!options.forward_agent) {
1822 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001823 error("Warning: this is probably a break-in attempt by a "
1824 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001825 return NULL;
1826 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001827 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001828 if (sock < 0)
1829 return NULL;
1830 c = channel_new("authentication agent connection",
1831 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001832 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001833 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001834 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001835 return c;
1836}
1837
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001838int
1839client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1840{
1841 Channel *c;
1842 int fd;
1843
1844 if (tun_mode == SSH_TUNMODE_NO)
1845 return 0;
1846
1847 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001848 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001849 return -1;
1850 }
1851
1852 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1853
1854 /* Open local tunnel device */
1855 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1856 error("Tunnel device open failed.");
1857 return -1;
1858 }
1859
1860 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1861 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1862 c->datagram = 1;
1863
1864#if defined(SSH_TUN_FILTER)
1865 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1866 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001867 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001868#endif
1869
1870 packet_start(SSH2_MSG_CHANNEL_OPEN);
1871 packet_put_cstring("tun@openssh.com");
1872 packet_put_int(c->self);
1873 packet_put_int(c->local_window_max);
1874 packet_put_int(c->local_maxpacket);
1875 packet_put_int(tun_mode);
1876 packet_put_int(remote_tun);
1877 packet_send();
1878
1879 return 0;
1880}
1881
Damien Millerbd483e72000-04-30 10:00:53 +10001882/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001883static void
Damien Miller630d6f42002-01-22 23:17:30 +11001884client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001885{
1886 Channel *c = NULL;
1887 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001888 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001889 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001890
1891 ctype = packet_get_string(&len);
1892 rchan = packet_get_int();
1893 rwindow = packet_get_int();
1894 rmaxpack = packet_get_int();
1895
Damien Millere247cc42000-05-07 12:03:14 +10001896 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001897 ctype, rchan, rwindow, rmaxpack);
1898
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1900 c = client_request_forwarded_tcpip(ctype, rchan);
1901 } else if (strcmp(ctype, "x11") == 0) {
1902 c = client_request_x11(ctype, rchan);
1903 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1904 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001905 }
1906/* XXX duplicate : */
1907 if (c != NULL) {
1908 debug("confirm %s", ctype);
1909 c->remote_id = rchan;
1910 c->remote_window = rwindow;
1911 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001912 if (c->type != SSH_CHANNEL_CONNECTING) {
1913 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1914 packet_put_int(c->remote_id);
1915 packet_put_int(c->self);
1916 packet_put_int(c->local_window);
1917 packet_put_int(c->local_maxpacket);
1918 packet_send();
1919 }
Damien Millerbd483e72000-04-30 10:00:53 +10001920 } else {
1921 debug("failure %s", ctype);
1922 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1923 packet_put_int(rchan);
1924 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001925 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001926 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001927 packet_put_cstring("");
1928 }
Damien Millerbd483e72000-04-30 10:00:53 +10001929 packet_send();
1930 }
1931 xfree(ctype);
1932}
Ben Lindstrombba81212001-06-25 05:01:22 +00001933static void
Damien Miller630d6f42002-01-22 23:17:30 +11001934client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001935{
1936 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001937 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001938 char *rtype;
1939
1940 id = packet_get_int();
1941 rtype = packet_get_string(NULL);
1942 reply = packet_get_char();
1943
1944 debug("client_input_channel_req: channel %d rtype %s reply %d",
1945 id, rtype, reply);
1946
Damien Miller3bbd8782004-06-18 22:23:22 +10001947 if (id == -1) {
1948 error("client_input_channel_req: request for channel -1");
1949 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001950 error("client_input_channel_req: channel %d: "
1951 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001952 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1953 packet_check_eom();
1954 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001955 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001956 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001957 if (c->ctl_chan != -1) {
1958 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001959 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001960 } else if (id == session_ident) {
1961 /* Record exit value of local session */
1962 success = 1;
1963 exit_status = exitval;
1964 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001965 /* Probably for a mux channel that has already closed */
1966 debug("%s: no sink for exit-status on channel %d",
1967 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001968 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001969 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001970 }
Damien Millerd0fdd682010-12-01 12:02:14 +11001971 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001972 packet_start(success ?
1973 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001974 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001975 packet_send();
1976 }
1977 xfree(rtype);
1978}
Damien Millerc3fa4072002-01-22 23:21:58 +11001979static void
1980client_input_global_request(int type, u_int32_t seq, void *ctxt)
1981{
1982 char *rtype;
1983 int want_reply;
1984 int success = 0;
1985
1986 rtype = packet_get_string(NULL);
1987 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001988 debug("client_input_global_request: rtype %s want_reply %d",
1989 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001990 if (want_reply) {
1991 packet_start(success ?
1992 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1993 packet_send();
1994 packet_write_wait();
1995 }
1996 xfree(rtype);
1997}
Damien Millerbd483e72000-04-30 10:00:53 +10001998
Damien Miller0e220db2004-06-15 10:34:08 +10001999void
Darren Tuckerfc959702004-07-17 16:12:08 +10002000client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002001 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002002{
2003 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002004 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002005
2006 debug2("%s: id %d", __func__, id);
2007
Darren Tucker5d78de62004-11-05 20:35:44 +11002008 if ((c = channel_lookup(id)) == NULL)
2009 fatal("client_session2_setup: channel %d: unknown channel", id);
2010
Damien Miller0dac6fb2010-11-20 15:19:38 +11002011 packet_set_interactive(want_tty,
2012 options.ip_qos_interactive, options.ip_qos_bulk);
2013
Damien Miller0e220db2004-06-15 10:34:08 +10002014 if (want_tty) {
2015 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002016
2017 /* Store window size in the packet. */
2018 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2019 memset(&ws, 0, sizeof(ws));
2020
Damien Miller5771ed72008-05-19 15:35:33 +10002021 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002022 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002023 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002024 packet_put_int((u_int)ws.ws_col);
2025 packet_put_int((u_int)ws.ws_row);
2026 packet_put_int((u_int)ws.ws_xpixel);
2027 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002028 if (tiop == NULL)
2029 tiop = get_saved_tio();
2030 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002031 packet_send();
2032 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002033 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002034 }
2035
2036 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002037 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002038 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002039 char *name, *val;
2040
2041 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002042 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002043 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002044 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002045 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002046 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002047 continue;
2048 }
2049 *val++ = '\0';
2050
2051 matched = 0;
2052 for (j = 0; j < options.num_send_env; j++) {
2053 if (match_pattern(name, options.send_env[j])) {
2054 matched = 1;
2055 break;
2056 }
2057 }
2058 if (!matched) {
2059 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002060 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002061 continue;
2062 }
2063
2064 debug("Sending env %s = %s", name, val);
2065 channel_request_start(id, "env", 0);
2066 packet_put_cstring(name);
2067 packet_put_cstring(val);
2068 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002069 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002070 }
2071 }
2072
2073 len = buffer_len(cmd);
2074 if (len > 0) {
2075 if (len > 900)
2076 len = 900;
2077 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002078 debug("Sending subsystem: %.*s",
2079 len, (u_char*)buffer_ptr(cmd));
2080 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002081 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002082 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002083 debug("Sending command: %.*s",
2084 len, (u_char*)buffer_ptr(cmd));
2085 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002086 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002087 }
2088 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2089 packet_send();
2090 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002091 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002092 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002093 packet_send();
2094 }
2095}
2096
Ben Lindstrombba81212001-06-25 05:01:22 +00002097static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002098client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002099{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002100 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002101
Damien Miller1383bd82000-04-06 12:32:37 +10002102 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2103 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2104 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2105 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002106 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002107 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2108 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002109 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002110 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002111 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2112 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002113 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002114
2115 /* rekeying */
2116 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002117
2118 /* global request reply messages */
2119 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2120 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002121}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002122
Ben Lindstrombba81212001-06-25 05:01:22 +00002123static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002124client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002125{
2126 dispatch_init(NULL);
2127 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2128 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2129 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002130 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2131 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2132 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002133 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2134 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2135 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002136
2137 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002138 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002139 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2140 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002141}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002142
Ben Lindstrombba81212001-06-25 05:01:22 +00002143static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002144client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002145{
2146 client_init_dispatch_13();
2147 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2148 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2149}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002150
Ben Lindstromdb47f382001-07-04 05:10:27 +00002151static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002152client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002153{
Damien Miller1383bd82000-04-06 12:32:37 +10002154 if (compat20)
2155 client_init_dispatch_20();
2156 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002157 client_init_dispatch_13();
2158 else
2159 client_init_dispatch_15();
2160}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002161
Damien Miller6c3eec72011-05-05 14:16:22 +10002162void
2163client_stop_mux(void)
2164{
2165 if (options.control_path != NULL && muxserver_sock != -1)
2166 unlink(options.control_path);
2167 /*
2168 * If we are in persist mode, signal that we should close when all
2169 * active channels are closed.
2170 */
Damien Miller4ac99c32011-06-20 14:43:31 +10002171 if (options.control_persist) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002172 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002173 setproctitle("[stopped mux]");
2174 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002175}
2176
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002177/* client specific fatal cleanup */
2178void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002179cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002180{
Damien Miller21771e22011-05-15 08:45:50 +10002181 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002182 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002183 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002184 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002185 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002186 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002187}