blob: 3de54281427cc7d0f88a714c9fc0960ae59e4517 [file] [log] [blame]
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001/* $OpenBSD: clientloop.c,v 1.246 2012/09/07 01:10:21 dtucker 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 Miller8d60be52012-02-11 08:18:17 +1100284#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
285static int
286client_x11_display_valid(const char *display)
287{
288 size_t i, dlen;
289
290 dlen = strlen(display);
291 for (i = 0; i < dlen; i++) {
292 if (!isalnum(display[i]) &&
293 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
294 debug("Invalid character '%c' in DISPLAY", display[i]);
295 return 0;
296 }
297 }
298 return 1;
299}
300
Damien Miller17e7ed02005-06-17 12:54:33 +1000301#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
302void
303client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000304 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000305{
306 char cmd[1024];
307 char line[512];
308 char xdisplay[512];
309 static char proto[512], data[512];
310 FILE *f;
311 int got_data = 0, generated = 0, do_unlink = 0, i;
312 char *xauthdir, *xauthfile;
313 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000314 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000315
316 xauthdir = xauthfile = NULL;
317 *_proto = proto;
318 *_data = data;
319 proto[0] = data[0] = '\0';
320
321 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
322 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100323 } else if (!client_x11_display_valid(display)) {
324 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
325 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000326 } else {
327 if (display == NULL) {
328 debug("x11_get_proto: DISPLAY not set");
329 return;
330 }
331 /*
332 * Handle FamilyLocal case where $DISPLAY does
333 * not match an authorization entry. For this we
334 * just try "xauth list unix:displaynum.screennum".
335 * XXX: "localhost" match to determine FamilyLocal
336 * is not perfect.
337 */
338 if (strncmp(display, "localhost:", 10) == 0) {
339 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
340 display + 10);
341 display = xdisplay;
342 }
343 if (trusted == 0) {
344 xauthdir = xmalloc(MAXPATHLEN);
345 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100346 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000347 if (mkdtemp(xauthdir) != NULL) {
348 do_unlink = 1;
349 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
350 xauthdir);
351 snprintf(cmd, sizeof(cmd),
352 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000353 " untrusted timeout %u 2>" _PATH_DEVNULL,
354 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000355 debug2("x11_get_proto: %s", cmd);
356 if (system(cmd) == 0)
357 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000358 if (x11_refuse_time == 0) {
359 now = time(NULL) + 1;
360 if (UINT_MAX - timeout < now)
361 x11_refuse_time = UINT_MAX;
362 else
363 x11_refuse_time = now + timeout;
364 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000365 }
366 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000367
368 /*
369 * When in untrusted mode, we read the cookie only if it was
370 * successfully generated as an untrusted one in the step
371 * above.
372 */
373 if (trusted || generated) {
374 snprintf(cmd, sizeof(cmd),
375 "%s %s%s list %s 2>" _PATH_DEVNULL,
376 xauth_path,
377 generated ? "-f " : "" ,
378 generated ? xauthfile : "",
379 display);
380 debug2("x11_get_proto: %s", cmd);
381 f = popen(cmd, "r");
382 if (f && fgets(line, sizeof(line), f) &&
383 sscanf(line, "%*s %511s %511s", proto, data) == 2)
384 got_data = 1;
385 if (f)
386 pclose(f);
387 } else
388 error("Warning: untrusted X11 forwarding setup failed: "
389 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000390 }
391
392 if (do_unlink) {
393 unlink(xauthfile);
394 rmdir(xauthdir);
395 }
396 if (xauthdir)
397 xfree(xauthdir);
398 if (xauthfile)
399 xfree(xauthfile);
400
401 /*
402 * If we didn't get authentication data, just make up some
403 * data. The forwarding code will check the validity of the
404 * response anyway, and substitute this data. The X11
405 * server, however, will ignore this fake data and use
406 * whatever authentication mechanisms it was using otherwise
407 * for the local connection.
408 */
409 if (!got_data) {
410 u_int32_t rnd = 0;
411
412 logit("Warning: No xauth data; "
413 "using fake authentication data for X11 forwarding.");
414 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
415 for (i = 0; i < 16; i++) {
416 if (i % 4 == 0)
417 rnd = arc4random();
418 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
419 rnd & 0xff);
420 rnd >>= 8;
421 }
422 }
423}
424
Damien Miller5428f641999-11-25 11:54:57 +1100425/*
426 * This is called when the interactive is entered. This checks if there is
427 * an EOF coming on stdin. We must check this explicitly, as select() does
428 * not appear to wake up when redirecting from /dev/null.
429 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Ben Lindstrombba81212001-06-25 05:01:22 +0000431static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000432client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433{
Damien Miller95def091999-11-25 00:26:21 +1100434 int len;
435 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Damien Miller5428f641999-11-25 11:54:57 +1100437 /*
438 * If standard input is to be "redirected from /dev/null", we simply
439 * mark that we have seen an EOF and send an EOF message to the
440 * server. Otherwise, we try to read a single character; it appears
441 * that for some files, such /dev/null, select() never wakes up for
442 * read for this descriptor, which means that we never get EOF. This
443 * way we will get the EOF if stdin comes from /dev/null or similar.
444 */
Damien Miller95def091999-11-25 00:26:21 +1100445 if (stdin_null_flag) {
446 /* Fake EOF on stdin. */
447 debug("Sending eof.");
448 stdin_eof = 1;
449 packet_start(SSH_CMSG_EOF);
450 packet_send();
451 } else {
Damien Miller95def091999-11-25 00:26:21 +1100452 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453
Damien Miller95def091999-11-25 00:26:21 +1100454 /* Check for immediate EOF on stdin. */
455 len = read(fileno(stdin), buf, 1);
456 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000457 /*
458 * EOF. Record that we have seen it and send
459 * EOF to server.
460 */
Damien Miller95def091999-11-25 00:26:21 +1100461 debug("Sending eof.");
462 stdin_eof = 1;
463 packet_start(SSH_CMSG_EOF);
464 packet_send();
465 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100466 /*
467 * Got data. We must store the data in the buffer,
468 * and also process it as an escape character if
469 * appropriate.
470 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000471 if ((u_char) buf[0] == escape_char1)
472 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000473 else
Damien Miller95def091999-11-25 00:26:21 +1100474 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100475 }
Damien Miller95def091999-11-25 00:26:21 +1100476 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478}
479
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller5428f641999-11-25 11:54:57 +1100481/*
482 * Make packets from buffered stdin data, and buffer them for sending to the
483 * connection.
484 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000487client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000489 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 /* Send buffered stdin data to the server. */
492 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100493 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100494 len = buffer_len(&stdin_buffer);
495 /* Keep the packets at reasonable size. */
496 if (len > packet_get_maxsize())
497 len = packet_get_maxsize();
498 packet_start(SSH_CMSG_STDIN_DATA);
499 packet_put_string(buffer_ptr(&stdin_buffer), len);
500 packet_send();
501 buffer_consume(&stdin_buffer, len);
502 /* If we have a pending EOF, send it now. */
503 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
504 packet_start(SSH_CMSG_EOF);
505 packet_send();
506 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508}
509
Damien Miller5428f641999-11-25 11:54:57 +1100510/*
511 * Checks if the client window has changed, and sends a packet about it to
512 * the server if so. The actual change is detected elsewhere (by a software
513 * interrupt on Unix); this just checks the flag and sends a message if
514 * appropriate.
515 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Ben Lindstrombba81212001-06-25 05:01:22 +0000517static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000518client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519{
Damien Miller1383bd82000-04-06 12:32:37 +1000520 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller1383bd82000-04-06 12:32:37 +1000522 if (! received_window_change_signal)
523 return;
524 /** XXX race */
525 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Millerd3444942000-09-30 14:20:03 +1100527 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000528
529 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000530 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000531 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000532 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
533 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000534 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100535 packet_put_int((u_int)ws.ws_row);
536 packet_put_int((u_int)ws.ws_col);
537 packet_put_int((u_int)ws.ws_xpixel);
538 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000539 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541}
542
Damien Miller509b0102003-12-17 16:33:10 +1100543static void
544client_global_request_reply(int type, u_int32_t seq, void *ctxt)
545{
Darren Tucker9f407c42008-06-13 04:50:27 +1000546 struct global_confirm *gc;
547
548 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
549 return;
550 if (gc->cb != NULL)
551 gc->cb(type, seq, gc->ctx);
552 if (--gc->ref_count <= 0) {
553 TAILQ_REMOVE(&global_confirms, gc, entry);
554 bzero(gc, sizeof(*gc));
555 xfree(gc);
556 }
557
Darren Tuckerf7288d72009-06-21 18:12:20 +1000558 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100559}
560
561static void
562server_alive_check(void)
563{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000564 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100565 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000566 cleanup_exit(255);
567 }
Damien Miller509b0102003-12-17 16:33:10 +1100568 packet_start(SSH2_MSG_GLOBAL_REQUEST);
569 packet_put_cstring("keepalive@openssh.com");
570 packet_put_char(1); /* boolean: want reply */
571 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000572 /* Insert an empty placeholder to maintain ordering */
573 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100574}
575
Damien Miller5428f641999-11-25 11:54:57 +1100576/*
577 * Waits until the client can do something (some data becomes available on
578 * one of the file descriptors).
579 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000580static void
Damien Miller5e953212001-01-30 09:14:00 +1100581client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000582 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583{
Damien Miller509b0102003-12-17 16:33:10 +1100584 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000585 int timeout_secs;
Damien Miller6c6da332012-06-20 22:31:26 +1000586 time_t minwait_secs = 0;
Damien Miller509b0102003-12-17 16:33:10 +1100587 int ret;
588
Damien Miller5e953212001-01-30 09:14:00 +1100589 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000590 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
591 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller1383bd82000-04-06 12:32:37 +1000593 if (!compat20) {
594 /* Read from the connection, unless our buffers are full. */
595 if (buffer_len(&stdout_buffer) < buffer_high &&
596 buffer_len(&stderr_buffer) < buffer_high &&
597 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100598 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000599 /*
600 * Read from stdin, unless we have seen EOF or have very much
601 * buffered data to send to the server.
602 */
603 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100604 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000605
606 /* Select stdout/stderr if have data in buffer. */
607 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100608 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000609 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100610 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000611 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000612 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000613 if (session_closed && !channel_still_open() &&
614 !packet_have_data_to_write()) {
615 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100616 memset(*readsetp, 0, *nallocp);
617 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000618 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000619 } else {
620 FD_SET(connection_in, *readsetp);
621 }
Damien Miller1383bd82000-04-06 12:32:37 +1000622 }
623
Damien Miller95def091999-11-25 00:26:21 +1100624 /* Select server connection if have data to write to the server. */
625 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100626 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100627
Damien Miller5428f641999-11-25 11:54:57 +1100628 /*
629 * Wait for something to happen. This will suspend the process until
630 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000631 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100632 */
Damien Miller95def091999-11-25 00:26:21 +1100633
Damien Millere11e1ea2010-08-03 16:04:46 +1000634 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
635 if (options.server_alive_interval > 0 && compat20)
636 timeout_secs = options.server_alive_interval;
637 set_control_persist_exit_time();
638 if (control_persist_exit_time > 0) {
639 timeout_secs = MIN(timeout_secs,
640 control_persist_exit_time - time(NULL));
641 if (timeout_secs < 0)
642 timeout_secs = 0;
643 }
Damien Millera6508752012-04-22 11:21:10 +1000644 if (minwait_secs != 0)
645 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000646 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100647 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000648 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000649 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100650 tv.tv_usec = 0;
651 tvp = &tv;
652 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000653
Damien Miller509b0102003-12-17 16:33:10 +1100654 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
655 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100656 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000657
658 /*
659 * We have to clear the select masks, because we return.
660 * We have to return, because the mainloop checks for the flags
661 * set by the signal handlers.
662 */
Damien Miller79faeff2001-11-12 11:06:32 +1100663 memset(*readsetp, 0, *nallocp);
664 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000665
Damien Miller95def091999-11-25 00:26:21 +1100666 if (errno == EINTR)
667 return;
668 /* Note: we might still have data in the buffers. */
669 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
670 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100671 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100672 } else if (ret == 0)
673 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674}
675
Ben Lindstrombba81212001-06-25 05:01:22 +0000676static void
Damien Millerad833b32000-08-23 10:46:23 +1000677client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678{
Damien Miller95def091999-11-25 00:26:21 +1100679 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000680 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000681 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
682 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000683 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000684 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
685 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686
Damien Miller21771e22011-05-15 08:45:50 +1000687 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688
Damien Miller5428f641999-11-25 11:54:57 +1100689 /*
690 * Free (and clear) the buffer to reduce the amount of data that gets
691 * written to swap.
692 */
Damien Millerad833b32000-08-23 10:46:23 +1000693 buffer_free(bin);
694 buffer_free(bout);
695 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller95def091999-11-25 00:26:21 +1100697 /* Send the suspend signal to the program itself. */
698 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699
Darren Tucker5d78de62004-11-05 20:35:44 +1100700 /* Reset window sizes in case they have changed */
701 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702
Damien Miller95def091999-11-25 00:26:21 +1100703 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000704 buffer_init(bin);
705 buffer_init(bout);
706 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Damien Miller21771e22011-05-15 08:45:50 +1000708 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709}
710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100712client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000714 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000715 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
Damien Miller5428f641999-11-25 11:54:57 +1100717 /*
718 * Read input from the server, and add any such data to the buffer of
719 * the packet subsystem.
720 */
Damien Miller95def091999-11-25 00:26:21 +1100721 if (FD_ISSET(connection_in, readset)) {
722 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000723 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
724 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000725 /*
726 * Received EOF. The remote host has closed the
727 * connection.
728 */
729 snprintf(buf, sizeof buf,
730 "Connection to %.300s closed by remote host.\r\n",
731 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 quit_pending = 1;
734 return;
Damien Miller95def091999-11-25 00:26:21 +1100735 }
Damien Miller5428f641999-11-25 11:54:57 +1100736 /*
737 * There is a kernel bug on Solaris that causes select to
738 * sometimes wake up even though there is no data available.
739 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000740 if (len < 0 &&
741 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100742 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743
Damien Miller95def091999-11-25 00:26:21 +1100744 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000745 /*
746 * An error has encountered. Perhaps there is a
747 * network problem.
748 */
749 snprintf(buf, sizeof buf,
750 "Read from remote host %.300s: %.100s\r\n",
751 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100752 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100753 quit_pending = 1;
754 return;
755 }
756 packet_process_incoming(buf, len);
757 }
Damien Miller1383bd82000-04-06 12:32:37 +1000758}
759
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000760static void
Damien Miller5771ed72008-05-19 15:35:33 +1000761client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000762{
Damien Miller5771ed72008-05-19 15:35:33 +1000763 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
764 char errmsg[256];
765 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000766
Damien Miller555f3b82011-05-15 08:48:05 +1000767 /*
768 * If a TTY was explicitly requested, then a failure to allocate
769 * one is fatal.
770 */
771 if (cr->action == CONFIRM_TTY &&
772 (options.request_tty == REQUEST_TTY_FORCE ||
773 options.request_tty == REQUEST_TTY_YES))
774 cr->action = CONFIRM_CLOSE;
775
Damien Miller5771ed72008-05-19 15:35:33 +1000776 /* XXX supress on mux _client_ quietmode */
777 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100778 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000779
Damien Miller5771ed72008-05-19 15:35:33 +1000780 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
781 debug2("%s request accepted on channel %d",
782 cr->request_type, c->self);
783 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
784 if (tochan) {
785 snprintf(errmsg, sizeof(errmsg),
786 "%s request failed\r\n", cr->request_type);
787 } else {
788 snprintf(errmsg, sizeof(errmsg),
789 "%s request failed on channel %d",
790 cr->request_type, c->self);
791 }
792 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000793 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000794 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000795 /*
796 * If error occurred on mux client, append to
797 * their stderr.
798 */
799 if (tochan) {
800 buffer_append(&c->extended, errmsg,
801 strlen(errmsg));
802 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000803 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000804 if (cr->action == CONFIRM_TTY) {
805 /*
806 * If a TTY allocation error occurred, then arrange
807 * for the correct TTY to leave raw mode.
808 */
809 if (c->self == session_ident)
810 leave_raw_mode(0);
811 else
812 mux_tty_alloc_failed(c);
813 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000814 chan_read_failed(c);
815 chan_write_failed(c);
816 }
Damien Miller0e220db2004-06-15 10:34:08 +1000817 }
Damien Miller5771ed72008-05-19 15:35:33 +1000818 xfree(cr);
819}
Damien Miller0e220db2004-06-15 10:34:08 +1000820
Damien Miller5771ed72008-05-19 15:35:33 +1000821static void
822client_abandon_status_confirm(Channel *c, void *ctx)
823{
824 xfree(ctx);
825}
826
Damien Miller6d7b4372011-06-23 08:31:57 +1000827void
Damien Miller555f3b82011-05-15 08:48:05 +1000828client_expect_confirm(int id, const char *request,
829 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000830{
831 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
832
833 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000834 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000835
836 channel_register_status_confirm(id, client_status_confirm,
837 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000838}
839
Darren Tucker9f407c42008-06-13 04:50:27 +1000840void
841client_register_global_confirm(global_confirm_cb *cb, void *ctx)
842{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000843 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000844
845 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000846 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
847 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
848 if (++last_gc->ref_count >= INT_MAX)
849 fatal("%s: last_gc->ref_count = %d",
850 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000851 return;
852 }
853
854 gc = xmalloc(sizeof(*gc));
855 gc->cb = cb;
856 gc->ctx = ctx;
857 gc->ref_count = 1;
858 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
859}
860
Damien Miller0e220db2004-06-15 10:34:08 +1000861static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000862process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000863{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000864 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100865 char *s, *cmd, *cancel_host;
Damien Millerff773642011-09-22 21:39:48 +1000866 int delete = 0, local = 0, remote = 0, dynamic = 0;
867 int cancel_port, ok;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100868 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000869
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100870 bzero(&fwd, sizeof(fwd));
871 fwd.listen_host = fwd.connect_host = NULL;
872
Damien Miller21771e22011-05-15 08:45:50 +1000873 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000874 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000875 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000876 if (s == NULL)
877 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100878 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000879 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000880 if (*s == '-')
881 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000882 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000883 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000884
Darren Tucker1973c882004-05-24 10:34:36 +1000885 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000886 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000887 logit(" -L[bind_address:]port:host:hostport "
888 "Request local forward");
889 logit(" -R[bind_address:]port:host:hostport "
890 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100891 logit(" -D[bind_address:]port "
892 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000893 logit(" -KL[bind_address:]port "
894 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000895 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000896 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000897 logit(" -KD[bind_address:]port "
898 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100899 if (!options.permit_local_command)
900 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000901 logit(" !args "
902 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100903 goto out;
904 }
905
906 if (*s == '!' && options.permit_local_command) {
907 s++;
908 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000909 goto out;
910 }
911
912 if (*s == 'K') {
913 delete = 1;
914 s++;
915 }
Damien Miller0164cb82008-11-05 16:30:31 +1100916 if (*s == 'L')
917 local = 1;
918 else if (*s == 'R')
919 remote = 1;
920 else if (*s == 'D')
921 dynamic = 1;
922 else {
Damien Miller996acd22003-04-09 20:59:48 +1000923 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000924 goto out;
925 }
Damien Miller0164cb82008-11-05 16:30:31 +1100926
Damien Millerff773642011-09-22 21:39:48 +1000927 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000928 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000929 goto out;
930 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000931
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100932 while (isspace(*++s))
933 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000934
Damien Millere1537f92010-01-26 13:26:22 +1100935 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000936 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100937 cancel_port = 0;
938 cancel_host = hpdelim(&s); /* may be NULL */
939 if (s != NULL) {
940 cancel_port = a2port(s);
941 cancel_host = cleanhostname(cancel_host);
942 } else {
943 cancel_port = a2port(cancel_host);
944 cancel_host = NULL;
945 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100946 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100947 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000948 goto out;
949 }
Damien Millerff773642011-09-22 21:39:48 +1000950 if (remote)
951 ok = channel_request_rforward_cancel(cancel_host,
952 cancel_port) == 0;
953 else if (dynamic)
954 ok = channel_cancel_lport_listener(cancel_host,
955 cancel_port, 0, options.gateway_ports) > 0;
956 else
957 ok = channel_cancel_lport_listener(cancel_host,
958 cancel_port, CHANNEL_CANCEL_PORT_STATIC,
959 options.gateway_ports) > 0;
960 if (!ok) {
961 logit("Unkown port forwarding.");
962 goto out;
963 }
964 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000965 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100966 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000967 logit("Bad forwarding specification.");
968 goto out;
969 }
Damien Miller0164cb82008-11-05 16:30:31 +1100970 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100971 if (channel_setup_local_fwd_listener(fwd.listen_host,
972 fwd.listen_port, fwd.connect_host,
973 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000974 logit("Port forwarding failed.");
975 goto out;
976 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100977 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000978 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100979 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000980 fwd.connect_port) < 0) {
981 logit("Port forwarding failed.");
982 goto out;
983 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100984 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000985 logit("Forwarding port.");
986 }
987
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000988out:
989 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000990 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000991 if (cmd)
992 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100993 if (fwd.listen_host != NULL)
994 xfree(fwd.listen_host);
995 if (fwd.connect_host != NULL)
996 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000997}
998
Darren Tucker92a39cf2012-09-07 11:20:20 +1000999/* reasons to suppress output of an escape command in help output */
1000#define SUPPRESS_NEVER 0 /* never suppress, always show */
1001#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
1002#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
1003#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
1004#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1005struct escape_help_text {
1006 const char *cmd;
1007 const char *text;
1008 unsigned int flags;
1009};
1010static struct escape_help_text esc_txt[] = {
1011 {".", "terminate session", SUPPRESS_MUXMASTER},
1012 {".", "terminate connection (and any multiplexed sessions)",
1013 SUPPRESS_MUXCLIENT},
1014 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1015 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1016 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001017 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001018 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1019 {"#", "list forwarded connections", SUPPRESS_NEVER},
1020 {"&", "background ssh (when waiting for connections to terminate)",
1021 SUPPRESS_MUXCLIENT},
1022 {"?", "this message", SUPPRESS_NEVER},
1023};
1024
1025static void
1026print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1027 int using_stderr)
1028{
1029 unsigned int i, suppress_flags;
1030 char string[1024];
1031
1032 snprintf(string, sizeof string, "%c?\r\n"
1033 "Supported escape sequences:\r\n", escape_char);
1034 buffer_append(b, string, strlen(string));
1035
1036 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1037 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1038 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1039 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1040
1041 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1042 if (esc_txt[i].flags & suppress_flags)
1043 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001044 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001045 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1046 buffer_append(b, string, strlen(string));
1047 }
1048
1049 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001050 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001051 "(Note that escapes are only recognized immediately after "
1052 "newline.)\r\n", escape_char, escape_char);
1053 buffer_append(b, string, strlen(string));
1054}
1055
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001056/*
1057 * Process the characters one by one, call with c==NULL for proto1 case.
1058 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001059static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001060process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1061 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001062{
1063 char string[1024];
1064 pid_t pid;
1065 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001066 u_int i;
1067 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001068 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001069 int *escape_pendingp, escape_char;
1070 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001071
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001072 if (c == NULL) {
1073 escape_pendingp = &escape_pending1;
1074 escape_char = escape_char1;
1075 } else {
1076 if (c->filter_ctx == NULL)
1077 return 0;
1078 efc = (struct escape_filter_ctx *)c->filter_ctx;
1079 escape_pendingp = &efc->escape_pending;
1080 escape_char = efc->escape_char;
1081 }
1082
Damien Millereccb9de2005-06-17 12:59:34 +10001083 if (len <= 0)
1084 return (0);
1085
1086 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001087 /* Get one character at a time. */
1088 ch = buf[i];
1089
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001090 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001091 /* We have previously seen an escape character. */
1092 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001093 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001094
1095 /* Process the escaped character. */
1096 switch (ch) {
1097 case '.':
1098 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 snprintf(string, sizeof string, "%c.\r\n",
1100 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001101 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001102
Damien Millere1537f92010-01-26 13:26:22 +11001103 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001104 chan_read_failed(c);
1105 chan_write_failed(c);
Darren Tucker66cb0e02012-09-06 21:19:05 +10001106 mux_master_session_cleanup_cb(c->self,
1107 NULL);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001108 return 0;
1109 } else
1110 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001111 return -1;
1112
1113 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001114 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001115 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001116 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001117 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001118 if (ch == 'Z' - 64)
1119 snprintf(b, sizeof b, "^Z");
1120 else
1121 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001122 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001123 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001124 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001125 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001126 buffer_append(berr, string,
1127 strlen(string));
1128 continue;
1129 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001130 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001131 snprintf(string, sizeof string,
1132 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001133 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001134
1135 /* Restore terminal modes and suspend. */
1136 client_suspend_self(bin, bout, berr);
1137
1138 /* We have been continued. */
1139 continue;
1140
Damien Miller54c45982003-05-15 10:20:13 +10001141 case 'B':
1142 if (compat20) {
1143 snprintf(string, sizeof string,
1144 "%cB\r\n", escape_char);
1145 buffer_append(berr, string,
1146 strlen(string));
1147 channel_request_start(session_ident,
1148 "break", 0);
1149 packet_put_int(1000);
1150 packet_send();
1151 }
1152 continue;
1153
Ben Lindstromf28f6342001-04-04 02:03:04 +00001154 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001155 if (compat20) {
1156 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001157 logit("Server does not "
1158 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001159 else
1160 need_rekeying = 1;
1161 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001162 continue;
1163
Darren Tucker50a48d02012-09-06 21:25:37 +10001164 case 'V':
1165 /* FALLTHROUGH */
1166 case 'v':
1167 if (c && c->ctl_chan != -1)
1168 goto noescape;
1169 if (!log_is_on_stderr()) {
1170 snprintf(string, sizeof string,
1171 "%c%c [Logging to syslog]\r\n",
1172 escape_char, ch);
1173 buffer_append(berr, string,
1174 strlen(string));
1175 continue;
1176 }
1177 if (ch == 'V' && options.log_level >
1178 SYSLOG_LEVEL_QUIET)
1179 log_change_level(--options.log_level);
1180 if (ch == 'v' && options.log_level <
1181 SYSLOG_LEVEL_DEBUG3)
1182 log_change_level(++options.log_level);
1183 snprintf(string, sizeof string,
1184 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1185 log_level_name(options.log_level));
1186 buffer_append(berr, string, strlen(string));
1187 continue;
1188
Damien Millerad833b32000-08-23 10:46:23 +10001189 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001190 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001191 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001192 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001193 * Detach the program (continue to serve
1194 * connections, but put in background and no
1195 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001196 */
Damien Miller96507ef2001-11-12 10:52:25 +11001197 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001198 leave_raw_mode(
1199 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001200
1201 /* Stop listening for new connections. */
1202 channel_stop_listening();
1203
1204 snprintf(string, sizeof string,
1205 "%c& [backgrounded]\n", escape_char);
1206 buffer_append(berr, string, strlen(string));
1207
1208 /* Fork into background. */
1209 pid = fork();
1210 if (pid < 0) {
1211 error("fork: %.100s", strerror(errno));
1212 continue;
1213 }
1214 if (pid != 0) { /* This is the parent. */
1215 /* The parent just exits. */
1216 exit(0);
1217 }
1218 /* The child continues serving connections. */
1219 if (compat20) {
1220 buffer_append(bin, "\004", 1);
1221 /* fake EOF on stdin */
1222 return -1;
1223 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001224 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001225 * Sending SSH_CMSG_EOF alone does not
1226 * always appear to be enough. So we
1227 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001228 */
1229 packet_start(SSH_CMSG_STDIN_DATA);
1230 packet_put_string("\004", 1);
1231 packet_send();
1232 /* Close stdin. */
1233 stdin_eof = 1;
1234 if (buffer_len(bin) == 0) {
1235 packet_start(SSH_CMSG_EOF);
1236 packet_send();
1237 }
1238 }
Damien Miller96507ef2001-11-12 10:52:25 +11001239 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001240
1241 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001242 print_escape_help(berr, escape_char, compat20,
1243 (c && c->ctl_chan != -1),
1244 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001245 continue;
1246
1247 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001248 snprintf(string, sizeof string, "%c#\r\n",
1249 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001250 buffer_append(berr, string, strlen(string));
1251 s = channel_open_message();
1252 buffer_append(berr, s, strlen(s));
1253 xfree(s);
1254 continue;
1255
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001256 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001257 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001258 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001259 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001260 continue;
1261
Damien Millerad833b32000-08-23 10:46:23 +10001262 default:
1263 if (ch != escape_char) {
1264 buffer_put_char(bin, escape_char);
1265 bytes++;
1266 }
1267 /* Escaped characters fall through here */
1268 break;
1269 }
1270 } else {
1271 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001272 * The previous character was not an escape char.
1273 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001274 */
1275 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001276 /*
1277 * It is. Set the flag and continue to
1278 * next character.
1279 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001280 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001281 continue;
1282 }
1283 }
1284
1285 /*
1286 * Normal character. Record whether it was a newline,
1287 * and append it to the buffer.
1288 */
1289 last_was_cr = (ch == '\r' || ch == '\n');
1290 buffer_put_char(bin, ch);
1291 bytes++;
1292 }
1293 return bytes;
1294}
1295
Ben Lindstrombba81212001-06-25 05:01:22 +00001296static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001297client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001298{
Damien Miller166fca82000-04-20 07:42:21 +10001299 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001300 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001301
Damien Miller95def091999-11-25 00:26:21 +11001302 /* Read input from stdin. */
1303 if (FD_ISSET(fileno(stdin), readset)) {
1304 /* Read as much as possible. */
1305 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001306 if (len < 0 &&
1307 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001308 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001309 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001310 /*
1311 * Received EOF or error. They are treated
1312 * similarly, except that an error message is printed
1313 * if it was an error condition.
1314 */
Damien Miller95def091999-11-25 00:26:21 +11001315 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001316 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1317 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001318 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001319 }
1320 /* Mark that we have seen EOF. */
1321 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001322 /*
1323 * Send an EOF message to the server unless there is
1324 * data in the buffer. If there is data in the
1325 * buffer, no message will be sent now. Code
1326 * elsewhere will send the EOF when the buffer
1327 * becomes empty if stdin_eof is set.
1328 */
Damien Miller95def091999-11-25 00:26:21 +11001329 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001330 packet_start(SSH_CMSG_EOF);
1331 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001332 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001333 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001334 /*
1335 * Normal successful read, and no escape character.
1336 * Just append the data to buffer.
1337 */
Damien Miller95def091999-11-25 00:26:21 +11001338 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001339 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001340 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001341 * Normal, successful read. But we have an escape
1342 * character and have to process the characters one
1343 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001344 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001345 if (process_escapes(NULL, &stdin_buffer,
1346 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001347 return;
Damien Miller95def091999-11-25 00:26:21 +11001348 }
1349 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350}
1351
Ben Lindstrombba81212001-06-25 05:01:22 +00001352static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001353client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354{
Damien Miller95def091999-11-25 00:26:21 +11001355 int len;
1356 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
Damien Miller95def091999-11-25 00:26:21 +11001358 /* Write buffered output to stdout. */
1359 if (FD_ISSET(fileno(stdout), writeset)) {
1360 /* Write as much data as possible. */
1361 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001362 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001363 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001364 if (errno == EINTR || errno == EAGAIN ||
1365 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001366 len = 0;
1367 else {
Damien Miller5428f641999-11-25 11:54:57 +11001368 /*
1369 * An error or EOF was encountered. Put an
1370 * error message to stderr buffer.
1371 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001372 snprintf(buf, sizeof buf,
1373 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001374 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001375 quit_pending = 1;
1376 return;
1377 }
1378 }
1379 /* Consume printed data from the buffer. */
1380 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381 }
Damien Miller95def091999-11-25 00:26:21 +11001382 /* Write buffered output to stderr. */
1383 if (FD_ISSET(fileno(stderr), writeset)) {
1384 /* Write as much data as possible. */
1385 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001386 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001387 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001388 if (errno == EINTR || errno == EAGAIN ||
1389 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001390 len = 0;
1391 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001392 /*
1393 * EOF or error, but can't even print
1394 * error message.
1395 */
Damien Miller95def091999-11-25 00:26:21 +11001396 quit_pending = 1;
1397 return;
1398 }
1399 }
1400 /* Consume printed characters from the buffer. */
1401 buffer_consume(&stderr_buffer, len);
1402 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403}
1404
Damien Miller5428f641999-11-25 11:54:57 +11001405/*
Damien Millerb38eff82000-04-01 11:09:21 +10001406 * Get packets from the connection input buffer, and process them as long as
1407 * there are packets available.
1408 *
1409 * Any unknown packets received during the actual
1410 * session cause the session to terminate. This is
1411 * intended to make debugging easier since no
1412 * confirmations are sent. Any compatible protocol
1413 * extensions must be negotiated during the
1414 * preparatory phase.
1415 */
1416
Ben Lindstrombba81212001-06-25 05:01:22 +00001417static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001418client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001419{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001420 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1421 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001422}
1423
Damien Millerad833b32000-08-23 10:46:23 +10001424/* scan buf[] for '~' before sending data to the peer */
1425
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001426/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1427void *
1428client_new_escape_filter_ctx(int escape_char)
1429{
1430 struct escape_filter_ctx *ret;
1431
1432 ret = xmalloc(sizeof(*ret));
1433 ret->escape_pending = 0;
1434 ret->escape_char = escape_char;
1435 return (void *)ret;
1436}
1437
Darren Tucker84c56f52008-06-13 04:55:46 +10001438/* Free the escape filter context on channel free */
1439void
1440client_filter_cleanup(int cid, void *ctx)
1441{
1442 xfree(ctx);
1443}
1444
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001445int
1446client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001447{
Damien Miller5771ed72008-05-19 15:35:33 +10001448 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1449 return 0;
1450
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001451 return process_escapes(c, &c->input, &c->output, &c->extended,
1452 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001453}
1454
Ben Lindstrombba81212001-06-25 05:01:22 +00001455static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001456client_channel_closed(int id, void *arg)
1457{
Damien Miller3ec27592001-10-12 11:35:04 +10001458 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001459 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001460 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001461}
1462
Damien Millerb38eff82000-04-01 11:09:21 +10001463/*
Damien Miller5428f641999-11-25 11:54:57 +11001464 * Implements the interactive session with the server. This is called after
1465 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001466 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1467 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001468 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Damien Miller4af51302000-04-16 11:18:38 +10001470int
Damien Millerad833b32000-08-23 10:46:23 +10001471client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472{
Damien Miller5e953212001-01-30 09:14:00 +11001473 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001474 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001475 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001476 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001477 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001478 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller95def091999-11-25 00:26:21 +11001480 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481
Damien Miller95def091999-11-25 00:26:21 +11001482 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001485 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001486 last_was_cr = 1;
1487 exit_status = -1;
1488 stdin_eof = 0;
1489 buffer_high = 64 * 1024;
1490 connection_in = packet_get_connection_in();
1491 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001492 max_fd = MAX(connection_in, connection_out);
1493
1494 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001495 /* enable nonblocking unless tty */
1496 if (!isatty(fileno(stdin)))
1497 set_nonblock(fileno(stdin));
1498 if (!isatty(fileno(stdout)))
1499 set_nonblock(fileno(stdout));
1500 if (!isatty(fileno(stderr)))
1501 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001502 max_fd = MAX(max_fd, fileno(stdin));
1503 max_fd = MAX(max_fd, fileno(stdout));
1504 max_fd = MAX(max_fd, fileno(stderr));
1505 }
Damien Miller95def091999-11-25 00:26:21 +11001506 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001507 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508
Damien Miller95def091999-11-25 00:26:21 +11001509 /* Initialize buffers. */
1510 buffer_init(&stdin_buffer);
1511 buffer_init(&stdout_buffer);
1512 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513
Damien Millerb38eff82000-04-01 11:09:21 +10001514 client_init_dispatch();
1515
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001516 /*
1517 * Set signal handlers, (e.g. to restore non-blocking mode)
1518 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1519 */
Darren Tucker07336da2004-11-05 20:02:16 +11001520 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1521 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001522 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1523 signal(SIGINT, signal_handler);
1524 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1525 signal(SIGQUIT, signal_handler);
1526 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1527 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001528 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
Damien Miller95def091999-11-25 00:26:21 +11001530 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001531 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001532
Ben Lindstrom5b828322001-02-09 01:34:36 +00001533 if (compat20) {
1534 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001535 if (session_ident != -1) {
1536 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1537 channel_register_filter(session_ident,
1538 client_simple_escape_filter, NULL,
1539 client_filter_cleanup,
1540 client_new_escape_filter_ctx(
1541 escape_char_arg));
1542 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001543 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001544 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001545 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001546 } else {
1547 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001548 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001549 }
Damien Millerad833b32000-08-23 10:46:23 +10001550
Damien Miller95def091999-11-25 00:26:21 +11001551 /* Main loop of the client for the interactive session mode. */
1552 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Damien Miller5428f641999-11-25 11:54:57 +11001554 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001555 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001557 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001558 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001559
1560 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001561
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001562 if (rekeying) {
1563 debug("rekeying in progress");
1564 } else {
1565 /*
1566 * Make packets of buffered stdin data, and buffer
1567 * them for sending to the server.
1568 */
1569 if (!compat20)
1570 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001572 /*
1573 * Make packets from buffered channel data, and
1574 * enqueue them for sending to the server.
1575 */
1576 if (packet_not_very_much_data_to_write())
1577 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001579 /*
1580 * Check if the window size has changed, and buffer a
1581 * message about it to the server if so.
1582 */
1583 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001584
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001585 if (quit_pending)
1586 break;
1587 }
Damien Miller5428f641999-11-25 11:54:57 +11001588 /*
1589 * Wait until we have something to do (something becomes
1590 * available on one of the descriptors).
1591 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001592 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001593 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001594 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Damien Miller95def091999-11-25 00:26:21 +11001596 if (quit_pending)
1597 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001599 /* Do channel operations unless rekeying in progress. */
1600 if (!rekeying) {
1601 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001602 if (need_rekeying || packet_need_rekeying()) {
1603 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001604 xxx_kex->done = 0;
1605 kex_send_kexinit(xxx_kex);
1606 need_rekeying = 0;
1607 }
1608 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001609
Damien Miller1383bd82000-04-06 12:32:37 +10001610 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001611 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612
Damien Miller1383bd82000-04-06 12:32:37 +10001613 if (quit_pending)
1614 break;
1615
1616 if (!compat20) {
1617 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001618 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001619 /*
1620 * Process output to stdout and stderr. Output to
1621 * the connection is processed elsewhere (above).
1622 */
Damien Miller5e953212001-01-30 09:14:00 +11001623 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001624 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625
Darren Tuckere32cf432010-01-08 16:51:40 +11001626 if (session_resumed) {
1627 connection_in = packet_get_connection_in();
1628 connection_out = packet_get_connection_out();
1629 max_fd = MAX(max_fd, connection_out);
1630 max_fd = MAX(max_fd, connection_in);
1631 session_resumed = 0;
1632 }
1633
Darren Tucker4d5cd332008-06-13 04:51:14 +10001634 /*
1635 * Send as much buffered packet data as possible to the
1636 * sender.
1637 */
Damien Miller5e953212001-01-30 09:14:00 +11001638 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001639 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001640
1641 /*
1642 * If we are a backgrounded control master, and the
1643 * timeout has expired without any active client
1644 * connections, then quit.
1645 */
1646 if (control_persist_exit_time > 0) {
1647 if (time(NULL) >= control_persist_exit_time) {
1648 debug("ControlPersist timeout expired");
1649 break;
1650 }
1651 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652 }
Damien Miller5e953212001-01-30 09:14:00 +11001653 if (readset)
1654 xfree(readset);
1655 if (writeset)
1656 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657
Damien Miller95def091999-11-25 00:26:21 +11001658 /* Terminate the session. */
1659
1660 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001661 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001662
Darren Tuckerde0c0252009-07-06 07:17:35 +10001663 if (compat20) {
1664 packet_start(SSH2_MSG_DISCONNECT);
1665 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1666 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001667 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001668 packet_send();
1669 packet_write_wait();
1670 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001671
Ben Lindstrom601e4362001-06-21 03:19:23 +00001672 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001673
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001674 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001675 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001676
1677 /* restore blocking io */
1678 if (!isatty(fileno(stdin)))
1679 unset_nonblock(fileno(stdin));
1680 if (!isatty(fileno(stdout)))
1681 unset_nonblock(fileno(stdout));
1682 if (!isatty(fileno(stderr)))
1683 unset_nonblock(fileno(stderr));
1684
Damien Millerd6965512003-12-17 16:31:53 +11001685 /*
1686 * If there was no shell or command requested, there will be no remote
1687 * exit status to be returned. In that case, clear error code if the
1688 * connection was deliberately terminated at this end.
1689 */
1690 if (no_shell_flag && received_signal == SIGTERM) {
1691 received_signal = 0;
1692 exit_status = 0;
1693 }
1694
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001695 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001696 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001697
1698 /*
1699 * In interactive mode (with pseudo tty) display a message indicating
1700 * that the connection has been closed.
1701 */
1702 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001703 snprintf(buf, sizeof buf,
1704 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001705 buffer_append(&stderr_buffer, buf, strlen(buf));
1706 }
1707
Damien Miller95def091999-11-25 00:26:21 +11001708 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001709 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001710 len = atomicio(vwrite, fileno(stdout),
1711 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001712 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001713 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001714 else
1715 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001716 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717
Damien Miller95def091999-11-25 00:26:21 +11001718 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001719 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001720 len = atomicio(vwrite, fileno(stderr),
1721 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001722 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001723 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001724 else
1725 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001726 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Clear and free any buffers. */
1729 memset(buf, 0, sizeof(buf));
1730 buffer_free(&stdin_buffer);
1731 buffer_free(&stdout_buffer);
1732 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001733
Damien Miller95def091999-11-25 00:26:21 +11001734 /* Report bytes transferred, and transfer rates. */
1735 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001736 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1737 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1738 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001739 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001740 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001741 verbose("Bytes per second: sent %.1f, received %.1f",
1742 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001743 /* Return the exit status of the program. */
1744 debug("Exit status %d", exit_status);
1745 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001746}
Damien Millerb38eff82000-04-01 11:09:21 +10001747
1748/*********/
1749
Ben Lindstrombba81212001-06-25 05:01:22 +00001750static void
Damien Miller630d6f42002-01-22 23:17:30 +11001751client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001752{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001753 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001754 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001755 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001756 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001757 memset(data, 0, data_len);
1758 xfree(data);
1759}
Ben Lindstrombba81212001-06-25 05:01:22 +00001760static void
Damien Miller630d6f42002-01-22 23:17:30 +11001761client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001762{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001763 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001764 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001765 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001766 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001767 memset(data, 0, data_len);
1768 xfree(data);
1769}
Ben Lindstrombba81212001-06-25 05:01:22 +00001770static void
Damien Miller630d6f42002-01-22 23:17:30 +11001771client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001772{
Damien Millerb38eff82000-04-01 11:09:21 +10001773 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001774 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001775 /* Acknowledge the exit. */
1776 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1777 packet_send();
1778 /*
1779 * Must wait for packet to be sent since we are
1780 * exiting the loop.
1781 */
1782 packet_write_wait();
1783 /* Flag that we want to exit. */
1784 quit_pending = 1;
1785}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001786static void
1787client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1788{
1789 Channel *c = NULL;
1790 int remote_id, sock;
1791
1792 /* Read the remote channel number from the message. */
1793 remote_id = packet_get_int();
1794 packet_check_eom();
1795
1796 /*
1797 * Get a connection to the local authentication agent (this may again
1798 * get forwarded).
1799 */
1800 sock = ssh_get_authentication_socket();
1801
1802 /*
1803 * If we could not connect the agent, send an error message back to
1804 * the server. This should never happen unless the agent dies,
1805 * because authentication forwarding is only enabled if we have an
1806 * agent.
1807 */
1808 if (sock >= 0) {
1809 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1810 -1, 0, 0, 0, "authentication agent connection", 1);
1811 c->remote_id = remote_id;
1812 c->force_drain = 1;
1813 }
1814 if (c == NULL) {
1815 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1816 packet_put_int(remote_id);
1817 } else {
1818 /* Send a confirmation to the remote host. */
1819 debug("Forwarding authentication connection.");
1820 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1821 packet_put_int(remote_id);
1822 packet_put_int(c->self);
1823 }
1824 packet_send();
1825}
Damien Millerb38eff82000-04-01 11:09:21 +10001826
Ben Lindstrombba81212001-06-25 05:01:22 +00001827static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001828client_request_forwarded_tcpip(const char *request_type, int rchan)
1829{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001830 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001831 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001832 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001833
1834 /* Get rest of the packet */
1835 listen_address = packet_get_string(NULL);
1836 listen_port = packet_get_int();
1837 originator_address = packet_get_string(NULL);
1838 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001839 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001840
Damien Millerbd740252008-05-19 15:37:09 +10001841 debug("client_request_forwarded_tcpip: listen %s port %d, "
1842 "originator %s port %d", listen_address, listen_port,
1843 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001844
Damien Millerbd740252008-05-19 15:37:09 +10001845 c = channel_connect_by_listen_address(listen_port,
1846 "forwarded-tcpip", originator_address);
1847
Damien Miller0bc1bd82000-11-13 22:57:25 +11001848 xfree(originator_address);
1849 xfree(listen_address);
1850 return c;
1851}
1852
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001853static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001854client_request_x11(const char *request_type, int rchan)
1855{
1856 Channel *c = NULL;
1857 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001858 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001859 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001860
1861 if (!options.forward_x11) {
1862 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001863 error("Warning: this is probably a break-in attempt by a "
1864 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001865 return NULL;
1866 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001867 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1868 verbose("Rejected X11 connection after ForwardX11Timeout "
1869 "expired");
1870 return NULL;
1871 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001872 originator = packet_get_string(NULL);
1873 if (datafellows & SSH_BUG_X11FWD) {
1874 debug2("buggy server: x11 request w/o originator_port");
1875 originator_port = 0;
1876 } else {
1877 originator_port = packet_get_int();
1878 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001879 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001880 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001881 debug("client_request_x11: request from %s %d", originator,
1882 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001883 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001884 sock = x11_connect_display();
1885 if (sock < 0)
1886 return NULL;
1887 c = channel_new("x11",
1888 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001889 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001890 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001891 return c;
1892}
1893
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001894static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001895client_request_agent(const char *request_type, int rchan)
1896{
1897 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001898 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899
1900 if (!options.forward_agent) {
1901 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001902 error("Warning: this is probably a break-in attempt by a "
1903 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 return NULL;
1905 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001906 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001907 if (sock < 0)
1908 return NULL;
1909 c = channel_new("authentication agent connection",
1910 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001911 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001912 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001913 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001914 return c;
1915}
1916
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001917int
1918client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1919{
1920 Channel *c;
1921 int fd;
1922
1923 if (tun_mode == SSH_TUNMODE_NO)
1924 return 0;
1925
1926 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001927 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001928 return -1;
1929 }
1930
1931 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1932
1933 /* Open local tunnel device */
1934 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1935 error("Tunnel device open failed.");
1936 return -1;
1937 }
1938
1939 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1940 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1941 c->datagram = 1;
1942
1943#if defined(SSH_TUN_FILTER)
1944 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1945 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001946 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001947#endif
1948
1949 packet_start(SSH2_MSG_CHANNEL_OPEN);
1950 packet_put_cstring("tun@openssh.com");
1951 packet_put_int(c->self);
1952 packet_put_int(c->local_window_max);
1953 packet_put_int(c->local_maxpacket);
1954 packet_put_int(tun_mode);
1955 packet_put_int(remote_tun);
1956 packet_send();
1957
1958 return 0;
1959}
1960
Damien Millerbd483e72000-04-30 10:00:53 +10001961/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001962static void
Damien Miller630d6f42002-01-22 23:17:30 +11001963client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001964{
1965 Channel *c = NULL;
1966 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001967 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001968 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001969
1970 ctype = packet_get_string(&len);
1971 rchan = packet_get_int();
1972 rwindow = packet_get_int();
1973 rmaxpack = packet_get_int();
1974
Damien Millere247cc42000-05-07 12:03:14 +10001975 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001976 ctype, rchan, rwindow, rmaxpack);
1977
Damien Miller0bc1bd82000-11-13 22:57:25 +11001978 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1979 c = client_request_forwarded_tcpip(ctype, rchan);
1980 } else if (strcmp(ctype, "x11") == 0) {
1981 c = client_request_x11(ctype, rchan);
1982 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1983 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001984 }
1985/* XXX duplicate : */
1986 if (c != NULL) {
1987 debug("confirm %s", ctype);
1988 c->remote_id = rchan;
1989 c->remote_window = rwindow;
1990 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001991 if (c->type != SSH_CHANNEL_CONNECTING) {
1992 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1993 packet_put_int(c->remote_id);
1994 packet_put_int(c->self);
1995 packet_put_int(c->local_window);
1996 packet_put_int(c->local_maxpacket);
1997 packet_send();
1998 }
Damien Millerbd483e72000-04-30 10:00:53 +10001999 } else {
2000 debug("failure %s", ctype);
2001 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2002 packet_put_int(rchan);
2003 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002004 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002005 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002006 packet_put_cstring("");
2007 }
Damien Millerbd483e72000-04-30 10:00:53 +10002008 packet_send();
2009 }
2010 xfree(ctype);
2011}
Ben Lindstrombba81212001-06-25 05:01:22 +00002012static void
Damien Miller630d6f42002-01-22 23:17:30 +11002013client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002014{
2015 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002016 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002017 char *rtype;
2018
2019 id = packet_get_int();
2020 rtype = packet_get_string(NULL);
2021 reply = packet_get_char();
2022
2023 debug("client_input_channel_req: channel %d rtype %s reply %d",
2024 id, rtype, reply);
2025
Damien Miller3bbd8782004-06-18 22:23:22 +10002026 if (id == -1) {
2027 error("client_input_channel_req: request for channel -1");
2028 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002029 error("client_input_channel_req: channel %d: "
2030 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002031 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2032 packet_check_eom();
2033 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002034 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002035 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002036 if (c->ctl_chan != -1) {
2037 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002038 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002039 } else if (id == session_ident) {
2040 /* Record exit value of local session */
2041 success = 1;
2042 exit_status = exitval;
2043 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002044 /* Probably for a mux channel that has already closed */
2045 debug("%s: no sink for exit-status on channel %d",
2046 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002047 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002048 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002049 }
Damien Millerd0fdd682010-12-01 12:02:14 +11002050 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002051 packet_start(success ?
2052 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002053 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002054 packet_send();
2055 }
2056 xfree(rtype);
2057}
Damien Millerc3fa4072002-01-22 23:21:58 +11002058static void
2059client_input_global_request(int type, u_int32_t seq, void *ctxt)
2060{
2061 char *rtype;
2062 int want_reply;
2063 int success = 0;
2064
2065 rtype = packet_get_string(NULL);
2066 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002067 debug("client_input_global_request: rtype %s want_reply %d",
2068 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002069 if (want_reply) {
2070 packet_start(success ?
2071 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2072 packet_send();
2073 packet_write_wait();
2074 }
2075 xfree(rtype);
2076}
Damien Millerbd483e72000-04-30 10:00:53 +10002077
Damien Miller0e220db2004-06-15 10:34:08 +10002078void
Darren Tuckerfc959702004-07-17 16:12:08 +10002079client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002080 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002081{
2082 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002083 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002084
2085 debug2("%s: id %d", __func__, id);
2086
Darren Tucker5d78de62004-11-05 20:35:44 +11002087 if ((c = channel_lookup(id)) == NULL)
2088 fatal("client_session2_setup: channel %d: unknown channel", id);
2089
Damien Miller0dac6fb2010-11-20 15:19:38 +11002090 packet_set_interactive(want_tty,
2091 options.ip_qos_interactive, options.ip_qos_bulk);
2092
Damien Miller0e220db2004-06-15 10:34:08 +10002093 if (want_tty) {
2094 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002095
2096 /* Store window size in the packet. */
2097 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2098 memset(&ws, 0, sizeof(ws));
2099
Damien Miller5771ed72008-05-19 15:35:33 +10002100 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002101 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002102 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002103 packet_put_int((u_int)ws.ws_col);
2104 packet_put_int((u_int)ws.ws_row);
2105 packet_put_int((u_int)ws.ws_xpixel);
2106 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002107 if (tiop == NULL)
2108 tiop = get_saved_tio();
2109 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002110 packet_send();
2111 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002112 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002113 }
2114
2115 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002116 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002117 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002118 char *name, *val;
2119
2120 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002121 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002122 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002123 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002124 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002125 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002126 continue;
2127 }
2128 *val++ = '\0';
2129
2130 matched = 0;
2131 for (j = 0; j < options.num_send_env; j++) {
2132 if (match_pattern(name, options.send_env[j])) {
2133 matched = 1;
2134 break;
2135 }
2136 }
2137 if (!matched) {
2138 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002139 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002140 continue;
2141 }
2142
2143 debug("Sending env %s = %s", name, val);
2144 channel_request_start(id, "env", 0);
2145 packet_put_cstring(name);
2146 packet_put_cstring(val);
2147 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002148 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002149 }
2150 }
2151
2152 len = buffer_len(cmd);
2153 if (len > 0) {
2154 if (len > 900)
2155 len = 900;
2156 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002157 debug("Sending subsystem: %.*s",
2158 len, (u_char*)buffer_ptr(cmd));
2159 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002160 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002161 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002162 debug("Sending command: %.*s",
2163 len, (u_char*)buffer_ptr(cmd));
2164 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002165 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002166 }
2167 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2168 packet_send();
2169 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002170 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002171 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002172 packet_send();
2173 }
2174}
2175
Ben Lindstrombba81212001-06-25 05:01:22 +00002176static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002177client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002178{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002179 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002180
Damien Miller1383bd82000-04-06 12:32:37 +10002181 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2182 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2183 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2184 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002185 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002186 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2187 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002188 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002189 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002190 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2191 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002192 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002193
2194 /* rekeying */
2195 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002196
2197 /* global request reply messages */
2198 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2199 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002200}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002201
Ben Lindstrombba81212001-06-25 05:01:22 +00002202static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002203client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002204{
2205 dispatch_init(NULL);
2206 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2207 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2208 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002209 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2210 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2211 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002212 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2213 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2214 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002215
2216 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002217 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002218 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2219 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002220}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002221
Ben Lindstrombba81212001-06-25 05:01:22 +00002222static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002223client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002224{
2225 client_init_dispatch_13();
2226 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2227 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2228}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002229
Ben Lindstromdb47f382001-07-04 05:10:27 +00002230static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002231client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002232{
Damien Miller1383bd82000-04-06 12:32:37 +10002233 if (compat20)
2234 client_init_dispatch_20();
2235 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002236 client_init_dispatch_13();
2237 else
2238 client_init_dispatch_15();
2239}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002240
Damien Miller6c3eec72011-05-05 14:16:22 +10002241void
2242client_stop_mux(void)
2243{
2244 if (options.control_path != NULL && muxserver_sock != -1)
2245 unlink(options.control_path);
2246 /*
2247 * If we are in persist mode, signal that we should close when all
2248 * active channels are closed.
2249 */
Damien Miller4ac99c32011-06-20 14:43:31 +10002250 if (options.control_persist) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002251 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002252 setproctitle("[stopped mux]");
2253 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002254}
2255
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002256/* client specific fatal cleanup */
2257void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002258cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002259{
Damien Miller21771e22011-05-15 08:45:50 +10002260 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002261 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002262 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002263 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002264 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002265 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002266}