blob: f69a9b025d49bd73dd06aadd966475264f5ce8a0 [file] [log] [blame]
Damien Miller8d60be52012-02-11 08:18:17 +11001/* $OpenBSD: clientloop.c,v 1.238 2012/01/18 21:46:43 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 Miller509b0102003-12-17 16:33:10 +1100586 int ret;
587
Damien Miller5e953212001-01-30 09:14:00 +1100588 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000589 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller1383bd82000-04-06 12:32:37 +1000591 if (!compat20) {
592 /* Read from the connection, unless our buffers are full. */
593 if (buffer_len(&stdout_buffer) < buffer_high &&
594 buffer_len(&stderr_buffer) < buffer_high &&
595 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100596 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000597 /*
598 * Read from stdin, unless we have seen EOF or have very much
599 * buffered data to send to the server.
600 */
601 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100602 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000603
604 /* Select stdout/stderr if have data in buffer. */
605 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100606 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000607 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100608 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000609 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000610 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000611 if (session_closed && !channel_still_open() &&
612 !packet_have_data_to_write()) {
613 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100614 memset(*readsetp, 0, *nallocp);
615 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000616 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000617 } else {
618 FD_SET(connection_in, *readsetp);
619 }
Damien Miller1383bd82000-04-06 12:32:37 +1000620 }
621
Damien Miller95def091999-11-25 00:26:21 +1100622 /* Select server connection if have data to write to the server. */
623 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100624 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100625
Damien Miller5428f641999-11-25 11:54:57 +1100626 /*
627 * Wait for something to happen. This will suspend the process until
628 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000629 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100630 */
Damien Miller95def091999-11-25 00:26:21 +1100631
Damien Millere11e1ea2010-08-03 16:04:46 +1000632 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
633 if (options.server_alive_interval > 0 && compat20)
634 timeout_secs = options.server_alive_interval;
635 set_control_persist_exit_time();
636 if (control_persist_exit_time > 0) {
637 timeout_secs = MIN(timeout_secs,
638 control_persist_exit_time - time(NULL));
639 if (timeout_secs < 0)
640 timeout_secs = 0;
641 }
642 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100643 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000644 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000645 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100646 tv.tv_usec = 0;
647 tvp = &tv;
648 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000649
Damien Miller509b0102003-12-17 16:33:10 +1100650 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
651 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100652 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000653
654 /*
655 * We have to clear the select masks, because we return.
656 * We have to return, because the mainloop checks for the flags
657 * set by the signal handlers.
658 */
Damien Miller79faeff2001-11-12 11:06:32 +1100659 memset(*readsetp, 0, *nallocp);
660 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000661
Damien Miller95def091999-11-25 00:26:21 +1100662 if (errno == EINTR)
663 return;
664 /* Note: we might still have data in the buffers. */
665 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
666 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100667 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100668 } else if (ret == 0)
669 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670}
671
Ben Lindstrombba81212001-06-25 05:01:22 +0000672static void
Damien Millerad833b32000-08-23 10:46:23 +1000673client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674{
Damien Miller95def091999-11-25 00:26:21 +1100675 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000676 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000677 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
678 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000679 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000680 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
681 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
Damien Miller21771e22011-05-15 08:45:50 +1000683 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller5428f641999-11-25 11:54:57 +1100685 /*
686 * Free (and clear) the buffer to reduce the amount of data that gets
687 * written to swap.
688 */
Damien Millerad833b32000-08-23 10:46:23 +1000689 buffer_free(bin);
690 buffer_free(bout);
691 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692
Damien Miller95def091999-11-25 00:26:21 +1100693 /* Send the suspend signal to the program itself. */
694 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695
Darren Tucker5d78de62004-11-05 20:35:44 +1100696 /* Reset window sizes in case they have changed */
697 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698
Damien Miller95def091999-11-25 00:26:21 +1100699 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000700 buffer_init(bin);
701 buffer_init(bout);
702 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703
Damien Miller21771e22011-05-15 08:45:50 +1000704 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100708client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000710 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000711 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller5428f641999-11-25 11:54:57 +1100713 /*
714 * Read input from the server, and add any such data to the buffer of
715 * the packet subsystem.
716 */
Damien Miller95def091999-11-25 00:26:21 +1100717 if (FD_ISSET(connection_in, readset)) {
718 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000719 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
720 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000721 /*
722 * Received EOF. The remote host has closed the
723 * connection.
724 */
725 snprintf(buf, sizeof buf,
726 "Connection to %.300s closed by remote host.\r\n",
727 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 quit_pending = 1;
730 return;
Damien Miller95def091999-11-25 00:26:21 +1100731 }
Damien Miller5428f641999-11-25 11:54:57 +1100732 /*
733 * There is a kernel bug on Solaris that causes select to
734 * sometimes wake up even though there is no data available.
735 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000736 if (len < 0 &&
737 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100738 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739
Damien Miller95def091999-11-25 00:26:21 +1100740 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000741 /*
742 * An error has encountered. Perhaps there is a
743 * network problem.
744 */
745 snprintf(buf, sizeof buf,
746 "Read from remote host %.300s: %.100s\r\n",
747 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100748 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100749 quit_pending = 1;
750 return;
751 }
752 packet_process_incoming(buf, len);
753 }
Damien Miller1383bd82000-04-06 12:32:37 +1000754}
755
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000756static void
Damien Miller5771ed72008-05-19 15:35:33 +1000757client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000758{
Damien Miller5771ed72008-05-19 15:35:33 +1000759 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
760 char errmsg[256];
761 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000762
Damien Miller555f3b82011-05-15 08:48:05 +1000763 /*
764 * If a TTY was explicitly requested, then a failure to allocate
765 * one is fatal.
766 */
767 if (cr->action == CONFIRM_TTY &&
768 (options.request_tty == REQUEST_TTY_FORCE ||
769 options.request_tty == REQUEST_TTY_YES))
770 cr->action = CONFIRM_CLOSE;
771
Damien Miller5771ed72008-05-19 15:35:33 +1000772 /* XXX supress on mux _client_ quietmode */
773 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100774 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000775
Damien Miller5771ed72008-05-19 15:35:33 +1000776 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
777 debug2("%s request accepted on channel %d",
778 cr->request_type, c->self);
779 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
780 if (tochan) {
781 snprintf(errmsg, sizeof(errmsg),
782 "%s request failed\r\n", cr->request_type);
783 } else {
784 snprintf(errmsg, sizeof(errmsg),
785 "%s request failed on channel %d",
786 cr->request_type, c->self);
787 }
788 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000789 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000790 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000791 /*
792 * If error occurred on mux client, append to
793 * their stderr.
794 */
795 if (tochan) {
796 buffer_append(&c->extended, errmsg,
797 strlen(errmsg));
798 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000799 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000800 if (cr->action == CONFIRM_TTY) {
801 /*
802 * If a TTY allocation error occurred, then arrange
803 * for the correct TTY to leave raw mode.
804 */
805 if (c->self == session_ident)
806 leave_raw_mode(0);
807 else
808 mux_tty_alloc_failed(c);
809 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000810 chan_read_failed(c);
811 chan_write_failed(c);
812 }
Damien Miller0e220db2004-06-15 10:34:08 +1000813 }
Damien Miller5771ed72008-05-19 15:35:33 +1000814 xfree(cr);
815}
Damien Miller0e220db2004-06-15 10:34:08 +1000816
Damien Miller5771ed72008-05-19 15:35:33 +1000817static void
818client_abandon_status_confirm(Channel *c, void *ctx)
819{
820 xfree(ctx);
821}
822
Damien Miller6d7b4372011-06-23 08:31:57 +1000823void
Damien Miller555f3b82011-05-15 08:48:05 +1000824client_expect_confirm(int id, const char *request,
825 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000826{
827 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
828
829 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000830 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000831
832 channel_register_status_confirm(id, client_status_confirm,
833 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000834}
835
Darren Tucker9f407c42008-06-13 04:50:27 +1000836void
837client_register_global_confirm(global_confirm_cb *cb, void *ctx)
838{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000839 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000840
841 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000842 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
843 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
844 if (++last_gc->ref_count >= INT_MAX)
845 fatal("%s: last_gc->ref_count = %d",
846 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000847 return;
848 }
849
850 gc = xmalloc(sizeof(*gc));
851 gc->cb = cb;
852 gc->ctx = ctx;
853 gc->ref_count = 1;
854 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
855}
856
Damien Miller0e220db2004-06-15 10:34:08 +1000857static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000858process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000859{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000860 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100861 char *s, *cmd, *cancel_host;
Damien Millerff773642011-09-22 21:39:48 +1000862 int delete = 0, local = 0, remote = 0, dynamic = 0;
863 int cancel_port, ok;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100864 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000865
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100866 bzero(&fwd, sizeof(fwd));
867 fwd.listen_host = fwd.connect_host = NULL;
868
Damien Miller21771e22011-05-15 08:45:50 +1000869 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000870 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000871 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000872 if (s == NULL)
873 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100874 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000875 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000876 if (*s == '-')
877 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000878 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000879 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000880
Darren Tucker1973c882004-05-24 10:34:36 +1000881 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000882 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000883 logit(" -L[bind_address:]port:host:hostport "
884 "Request local forward");
885 logit(" -R[bind_address:]port:host:hostport "
886 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100887 logit(" -D[bind_address:]port "
888 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000889 logit(" -KL[bind_address:]port "
890 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000891 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000892 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000893 logit(" -KD[bind_address:]port "
894 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100895 if (!options.permit_local_command)
896 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000897 logit(" !args "
898 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100899 goto out;
900 }
901
902 if (*s == '!' && options.permit_local_command) {
903 s++;
904 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000905 goto out;
906 }
907
908 if (*s == 'K') {
909 delete = 1;
910 s++;
911 }
Damien Miller0164cb82008-11-05 16:30:31 +1100912 if (*s == 'L')
913 local = 1;
914 else if (*s == 'R')
915 remote = 1;
916 else if (*s == 'D')
917 dynamic = 1;
918 else {
Damien Miller996acd22003-04-09 20:59:48 +1000919 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000920 goto out;
921 }
Damien Miller0164cb82008-11-05 16:30:31 +1100922
Damien Millerff773642011-09-22 21:39:48 +1000923 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000925 goto out;
926 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000927
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100928 while (isspace(*++s))
929 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000930
Damien Millere1537f92010-01-26 13:26:22 +1100931 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000932 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100933 cancel_port = 0;
934 cancel_host = hpdelim(&s); /* may be NULL */
935 if (s != NULL) {
936 cancel_port = a2port(s);
937 cancel_host = cleanhostname(cancel_host);
938 } else {
939 cancel_port = a2port(cancel_host);
940 cancel_host = NULL;
941 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100942 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100943 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000944 goto out;
945 }
Damien Millerff773642011-09-22 21:39:48 +1000946 if (remote)
947 ok = channel_request_rforward_cancel(cancel_host,
948 cancel_port) == 0;
949 else if (dynamic)
950 ok = channel_cancel_lport_listener(cancel_host,
951 cancel_port, 0, options.gateway_ports) > 0;
952 else
953 ok = channel_cancel_lport_listener(cancel_host,
954 cancel_port, CHANNEL_CANCEL_PORT_STATIC,
955 options.gateway_ports) > 0;
956 if (!ok) {
957 logit("Unkown port forwarding.");
958 goto out;
959 }
960 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000961 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100962 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000963 logit("Bad forwarding specification.");
964 goto out;
965 }
Damien Miller0164cb82008-11-05 16:30:31 +1100966 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100967 if (channel_setup_local_fwd_listener(fwd.listen_host,
968 fwd.listen_port, fwd.connect_host,
969 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000970 logit("Port forwarding failed.");
971 goto out;
972 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100973 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000974 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100975 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000976 fwd.connect_port) < 0) {
977 logit("Port forwarding failed.");
978 goto out;
979 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100980 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000981 logit("Forwarding port.");
982 }
983
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000984out:
985 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000986 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000987 if (cmd)
988 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100989 if (fwd.listen_host != NULL)
990 xfree(fwd.listen_host);
991 if (fwd.connect_host != NULL)
992 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000993}
994
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000995/*
996 * Process the characters one by one, call with c==NULL for proto1 case.
997 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000998static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000999process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1000 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001001{
1002 char string[1024];
1003 pid_t pid;
1004 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001005 u_int i;
1006 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001007 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001008 int *escape_pendingp, escape_char;
1009 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001010
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001011 if (c == NULL) {
1012 escape_pendingp = &escape_pending1;
1013 escape_char = escape_char1;
1014 } else {
1015 if (c->filter_ctx == NULL)
1016 return 0;
1017 efc = (struct escape_filter_ctx *)c->filter_ctx;
1018 escape_pendingp = &efc->escape_pending;
1019 escape_char = efc->escape_char;
1020 }
1021
Damien Millereccb9de2005-06-17 12:59:34 +10001022 if (len <= 0)
1023 return (0);
1024
1025 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001026 /* Get one character at a time. */
1027 ch = buf[i];
1028
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001029 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001030 /* We have previously seen an escape character. */
1031 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001032 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001033
1034 /* Process the escaped character. */
1035 switch (ch) {
1036 case '.':
1037 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001038 snprintf(string, sizeof string, "%c.\r\n",
1039 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001040 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001041
Damien Millere1537f92010-01-26 13:26:22 +11001042 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001043 chan_read_failed(c);
1044 chan_write_failed(c);
1045 return 0;
1046 } else
1047 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001048 return -1;
1049
1050 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001051 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001052 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001053 noescape:
1054 snprintf(string, sizeof string,
1055 "%c%c escape not available to "
1056 "multiplexed sessions\r\n",
1057 escape_char, ch);
1058 buffer_append(berr, string,
1059 strlen(string));
1060 continue;
1061 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001062 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001063 snprintf(string, sizeof string,
1064 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001065 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001066
1067 /* Restore terminal modes and suspend. */
1068 client_suspend_self(bin, bout, berr);
1069
1070 /* We have been continued. */
1071 continue;
1072
Damien Miller54c45982003-05-15 10:20:13 +10001073 case 'B':
1074 if (compat20) {
1075 snprintf(string, sizeof string,
1076 "%cB\r\n", escape_char);
1077 buffer_append(berr, string,
1078 strlen(string));
1079 channel_request_start(session_ident,
1080 "break", 0);
1081 packet_put_int(1000);
1082 packet_send();
1083 }
1084 continue;
1085
Ben Lindstromf28f6342001-04-04 02:03:04 +00001086 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001087 if (compat20) {
1088 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001089 logit("Server does not "
1090 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001091 else
1092 need_rekeying = 1;
1093 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001094 continue;
1095
Damien Millerad833b32000-08-23 10:46:23 +10001096 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001097 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001098 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001099 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001100 * Detach the program (continue to serve
1101 * connections, but put in background and no
1102 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001103 */
Damien Miller96507ef2001-11-12 10:52:25 +11001104 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001105 leave_raw_mode(
1106 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001107
1108 /* Stop listening for new connections. */
1109 channel_stop_listening();
1110
1111 snprintf(string, sizeof string,
1112 "%c& [backgrounded]\n", escape_char);
1113 buffer_append(berr, string, strlen(string));
1114
1115 /* Fork into background. */
1116 pid = fork();
1117 if (pid < 0) {
1118 error("fork: %.100s", strerror(errno));
1119 continue;
1120 }
1121 if (pid != 0) { /* This is the parent. */
1122 /* The parent just exits. */
1123 exit(0);
1124 }
1125 /* The child continues serving connections. */
1126 if (compat20) {
1127 buffer_append(bin, "\004", 1);
1128 /* fake EOF on stdin */
1129 return -1;
1130 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001131 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001132 * Sending SSH_CMSG_EOF alone does not
1133 * always appear to be enough. So we
1134 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001135 */
1136 packet_start(SSH_CMSG_STDIN_DATA);
1137 packet_put_string("\004", 1);
1138 packet_send();
1139 /* Close stdin. */
1140 stdin_eof = 1;
1141 if (buffer_len(bin) == 0) {
1142 packet_start(SSH_CMSG_EOF);
1143 packet_send();
1144 }
1145 }
Damien Miller96507ef2001-11-12 10:52:25 +11001146 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001147
1148 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001149 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001150 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001151"%c?\r\n\
1152Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001153 %c. - terminate session\r\n\
1154 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001155 %cR - Request rekey (SSH protocol 2 only)\r\n\
1156 %c# - list forwarded connections\r\n\
1157 %c? - this message\r\n\
1158 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001159(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001160 escape_char, escape_char,
1161 escape_char, escape_char,
1162 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001163 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001164 } else {
1165 snprintf(string, sizeof string,
1166"%c?\r\n\
1167Supported escape sequences:\r\n\
1168 %c. - terminate connection (and any multiplexed sessions)\r\n\
1169 %cB - send a BREAK to the remote system\r\n\
1170 %cC - open a command line\r\n\
1171 %cR - Request rekey (SSH protocol 2 only)\r\n\
1172 %c^Z - suspend ssh\r\n\
1173 %c# - list forwarded connections\r\n\
1174 %c& - background ssh (when waiting for connections to terminate)\r\n\
1175 %c? - this message\r\n\
1176 %c%c - send the escape character by typing it twice\r\n\
1177(Note that escapes are only recognized immediately after newline.)\r\n",
1178 escape_char, escape_char,
1179 escape_char, escape_char,
1180 escape_char, escape_char,
1181 escape_char, escape_char,
1182 escape_char, escape_char,
1183 escape_char);
1184 }
Damien Millerad833b32000-08-23 10:46:23 +10001185 buffer_append(berr, string, strlen(string));
1186 continue;
1187
1188 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001189 snprintf(string, sizeof string, "%c#\r\n",
1190 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001191 buffer_append(berr, string, strlen(string));
1192 s = channel_open_message();
1193 buffer_append(berr, s, strlen(s));
1194 xfree(s);
1195 continue;
1196
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001197 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001198 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001199 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001200 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001201 continue;
1202
Damien Millerad833b32000-08-23 10:46:23 +10001203 default:
1204 if (ch != escape_char) {
1205 buffer_put_char(bin, escape_char);
1206 bytes++;
1207 }
1208 /* Escaped characters fall through here */
1209 break;
1210 }
1211 } else {
1212 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001213 * The previous character was not an escape char.
1214 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001215 */
1216 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001217 /*
1218 * It is. Set the flag and continue to
1219 * next character.
1220 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001221 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001222 continue;
1223 }
1224 }
1225
1226 /*
1227 * Normal character. Record whether it was a newline,
1228 * and append it to the buffer.
1229 */
1230 last_was_cr = (ch == '\r' || ch == '\n');
1231 buffer_put_char(bin, ch);
1232 bytes++;
1233 }
1234 return bytes;
1235}
1236
Ben Lindstrombba81212001-06-25 05:01:22 +00001237static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001238client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001239{
Damien Miller166fca82000-04-20 07:42:21 +10001240 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001241 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001242
Damien Miller95def091999-11-25 00:26:21 +11001243 /* Read input from stdin. */
1244 if (FD_ISSET(fileno(stdin), readset)) {
1245 /* Read as much as possible. */
1246 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001247 if (len < 0 &&
1248 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001249 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001250 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001251 /*
1252 * Received EOF or error. They are treated
1253 * similarly, except that an error message is printed
1254 * if it was an error condition.
1255 */
Damien Miller95def091999-11-25 00:26:21 +11001256 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001257 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1258 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001259 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001260 }
1261 /* Mark that we have seen EOF. */
1262 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001263 /*
1264 * Send an EOF message to the server unless there is
1265 * data in the buffer. If there is data in the
1266 * buffer, no message will be sent now. Code
1267 * elsewhere will send the EOF when the buffer
1268 * becomes empty if stdin_eof is set.
1269 */
Damien Miller95def091999-11-25 00:26:21 +11001270 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271 packet_start(SSH_CMSG_EOF);
1272 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001273 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001274 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001275 /*
1276 * Normal successful read, and no escape character.
1277 * Just append the data to buffer.
1278 */
Damien Miller95def091999-11-25 00:26:21 +11001279 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001280 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001281 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001282 * Normal, successful read. But we have an escape
1283 * character and have to process the characters one
1284 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001285 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001286 if (process_escapes(NULL, &stdin_buffer,
1287 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001288 return;
Damien Miller95def091999-11-25 00:26:21 +11001289 }
1290 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291}
1292
Ben Lindstrombba81212001-06-25 05:01:22 +00001293static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001294client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295{
Damien Miller95def091999-11-25 00:26:21 +11001296 int len;
1297 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298
Damien Miller95def091999-11-25 00:26:21 +11001299 /* Write buffered output to stdout. */
1300 if (FD_ISSET(fileno(stdout), writeset)) {
1301 /* Write as much data as possible. */
1302 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001303 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001304 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001305 if (errno == EINTR || errno == EAGAIN ||
1306 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001307 len = 0;
1308 else {
Damien Miller5428f641999-11-25 11:54:57 +11001309 /*
1310 * An error or EOF was encountered. Put an
1311 * error message to stderr buffer.
1312 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001313 snprintf(buf, sizeof buf,
1314 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001315 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001316 quit_pending = 1;
1317 return;
1318 }
1319 }
1320 /* Consume printed data from the buffer. */
1321 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322 }
Damien Miller95def091999-11-25 00:26:21 +11001323 /* Write buffered output to stderr. */
1324 if (FD_ISSET(fileno(stderr), writeset)) {
1325 /* Write as much data as possible. */
1326 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001327 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001328 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001329 if (errno == EINTR || errno == EAGAIN ||
1330 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001331 len = 0;
1332 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001333 /*
1334 * EOF or error, but can't even print
1335 * error message.
1336 */
Damien Miller95def091999-11-25 00:26:21 +11001337 quit_pending = 1;
1338 return;
1339 }
1340 }
1341 /* Consume printed characters from the buffer. */
1342 buffer_consume(&stderr_buffer, len);
1343 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344}
1345
Damien Miller5428f641999-11-25 11:54:57 +11001346/*
Damien Millerb38eff82000-04-01 11:09:21 +10001347 * Get packets from the connection input buffer, and process them as long as
1348 * there are packets available.
1349 *
1350 * Any unknown packets received during the actual
1351 * session cause the session to terminate. This is
1352 * intended to make debugging easier since no
1353 * confirmations are sent. Any compatible protocol
1354 * extensions must be negotiated during the
1355 * preparatory phase.
1356 */
1357
Ben Lindstrombba81212001-06-25 05:01:22 +00001358static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001359client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001360{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001361 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1362 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001363}
1364
Damien Millerad833b32000-08-23 10:46:23 +10001365/* scan buf[] for '~' before sending data to the peer */
1366
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001367/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1368void *
1369client_new_escape_filter_ctx(int escape_char)
1370{
1371 struct escape_filter_ctx *ret;
1372
1373 ret = xmalloc(sizeof(*ret));
1374 ret->escape_pending = 0;
1375 ret->escape_char = escape_char;
1376 return (void *)ret;
1377}
1378
Darren Tucker84c56f52008-06-13 04:55:46 +10001379/* Free the escape filter context on channel free */
1380void
1381client_filter_cleanup(int cid, void *ctx)
1382{
1383 xfree(ctx);
1384}
1385
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001386int
1387client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001388{
Damien Miller5771ed72008-05-19 15:35:33 +10001389 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1390 return 0;
1391
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001392 return process_escapes(c, &c->input, &c->output, &c->extended,
1393 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001394}
1395
Ben Lindstrombba81212001-06-25 05:01:22 +00001396static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001397client_channel_closed(int id, void *arg)
1398{
Damien Miller3ec27592001-10-12 11:35:04 +10001399 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001400 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001401 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001402}
1403
Damien Millerb38eff82000-04-01 11:09:21 +10001404/*
Damien Miller5428f641999-11-25 11:54:57 +11001405 * Implements the interactive session with the server. This is called after
1406 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001407 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1408 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001409 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Damien Miller4af51302000-04-16 11:18:38 +10001411int
Damien Millerad833b32000-08-23 10:46:23 +10001412client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413{
Damien Miller5e953212001-01-30 09:14:00 +11001414 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001415 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001416 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001417 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001418 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001419 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Damien Miller95def091999-11-25 00:26:21 +11001421 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422
Damien Miller95def091999-11-25 00:26:21 +11001423 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424
Damien Miller95def091999-11-25 00:26:21 +11001425 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001426 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001427 last_was_cr = 1;
1428 exit_status = -1;
1429 stdin_eof = 0;
1430 buffer_high = 64 * 1024;
1431 connection_in = packet_get_connection_in();
1432 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001433 max_fd = MAX(connection_in, connection_out);
1434
1435 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001436 /* enable nonblocking unless tty */
1437 if (!isatty(fileno(stdin)))
1438 set_nonblock(fileno(stdin));
1439 if (!isatty(fileno(stdout)))
1440 set_nonblock(fileno(stdout));
1441 if (!isatty(fileno(stderr)))
1442 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001443 max_fd = MAX(max_fd, fileno(stdin));
1444 max_fd = MAX(max_fd, fileno(stdout));
1445 max_fd = MAX(max_fd, fileno(stderr));
1446 }
Damien Miller95def091999-11-25 00:26:21 +11001447 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001448 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller95def091999-11-25 00:26:21 +11001450 /* Initialize buffers. */
1451 buffer_init(&stdin_buffer);
1452 buffer_init(&stdout_buffer);
1453 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Damien Millerb38eff82000-04-01 11:09:21 +10001455 client_init_dispatch();
1456
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001457 /*
1458 * Set signal handlers, (e.g. to restore non-blocking mode)
1459 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1460 */
Darren Tucker07336da2004-11-05 20:02:16 +11001461 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1462 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001463 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1464 signal(SIGINT, signal_handler);
1465 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1466 signal(SIGQUIT, signal_handler);
1467 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1468 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001469 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
Damien Miller95def091999-11-25 00:26:21 +11001471 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001472 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473
Ben Lindstrom5b828322001-02-09 01:34:36 +00001474 if (compat20) {
1475 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001476 if (session_ident != -1) {
1477 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1478 channel_register_filter(session_ident,
1479 client_simple_escape_filter, NULL,
1480 client_filter_cleanup,
1481 client_new_escape_filter_ctx(
1482 escape_char_arg));
1483 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001484 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001485 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001486 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001487 } else {
1488 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001489 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001490 }
Damien Millerad833b32000-08-23 10:46:23 +10001491
Damien Miller95def091999-11-25 00:26:21 +11001492 /* Main loop of the client for the interactive session mode. */
1493 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller5428f641999-11-25 11:54:57 +11001495 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001496 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001498 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001499 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001500
1501 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001502
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001503 if (rekeying) {
1504 debug("rekeying in progress");
1505 } else {
1506 /*
1507 * Make packets of buffered stdin data, and buffer
1508 * them for sending to the server.
1509 */
1510 if (!compat20)
1511 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001513 /*
1514 * Make packets from buffered channel data, and
1515 * enqueue them for sending to the server.
1516 */
1517 if (packet_not_very_much_data_to_write())
1518 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001520 /*
1521 * Check if the window size has changed, and buffer a
1522 * message about it to the server if so.
1523 */
1524 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001526 if (quit_pending)
1527 break;
1528 }
Damien Miller5428f641999-11-25 11:54:57 +11001529 /*
1530 * Wait until we have something to do (something becomes
1531 * available on one of the descriptors).
1532 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001533 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001534 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001535 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Damien Miller95def091999-11-25 00:26:21 +11001537 if (quit_pending)
1538 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001540 /* Do channel operations unless rekeying in progress. */
1541 if (!rekeying) {
1542 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001543 if (need_rekeying || packet_need_rekeying()) {
1544 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001545 xxx_kex->done = 0;
1546 kex_send_kexinit(xxx_kex);
1547 need_rekeying = 0;
1548 }
1549 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550
Damien Miller1383bd82000-04-06 12:32:37 +10001551 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001552 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Damien Miller1383bd82000-04-06 12:32:37 +10001554 if (quit_pending)
1555 break;
1556
1557 if (!compat20) {
1558 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001559 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001560 /*
1561 * Process output to stdout and stderr. Output to
1562 * the connection is processed elsewhere (above).
1563 */
Damien Miller5e953212001-01-30 09:14:00 +11001564 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566
Darren Tuckere32cf432010-01-08 16:51:40 +11001567 if (session_resumed) {
1568 connection_in = packet_get_connection_in();
1569 connection_out = packet_get_connection_out();
1570 max_fd = MAX(max_fd, connection_out);
1571 max_fd = MAX(max_fd, connection_in);
1572 session_resumed = 0;
1573 }
1574
Darren Tucker4d5cd332008-06-13 04:51:14 +10001575 /*
1576 * Send as much buffered packet data as possible to the
1577 * sender.
1578 */
Damien Miller5e953212001-01-30 09:14:00 +11001579 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001580 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001581
1582 /*
1583 * If we are a backgrounded control master, and the
1584 * timeout has expired without any active client
1585 * connections, then quit.
1586 */
1587 if (control_persist_exit_time > 0) {
1588 if (time(NULL) >= control_persist_exit_time) {
1589 debug("ControlPersist timeout expired");
1590 break;
1591 }
1592 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593 }
Damien Miller5e953212001-01-30 09:14:00 +11001594 if (readset)
1595 xfree(readset);
1596 if (writeset)
1597 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598
Damien Miller95def091999-11-25 00:26:21 +11001599 /* Terminate the session. */
1600
1601 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001602 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001603
Darren Tuckerde0c0252009-07-06 07:17:35 +10001604 if (compat20) {
1605 packet_start(SSH2_MSG_DISCONNECT);
1606 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1607 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001608 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001609 packet_send();
1610 packet_write_wait();
1611 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001612
Ben Lindstrom601e4362001-06-21 03:19:23 +00001613 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001614
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001615 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001616 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001617
1618 /* restore blocking io */
1619 if (!isatty(fileno(stdin)))
1620 unset_nonblock(fileno(stdin));
1621 if (!isatty(fileno(stdout)))
1622 unset_nonblock(fileno(stdout));
1623 if (!isatty(fileno(stderr)))
1624 unset_nonblock(fileno(stderr));
1625
Damien Millerd6965512003-12-17 16:31:53 +11001626 /*
1627 * If there was no shell or command requested, there will be no remote
1628 * exit status to be returned. In that case, clear error code if the
1629 * connection was deliberately terminated at this end.
1630 */
1631 if (no_shell_flag && received_signal == SIGTERM) {
1632 received_signal = 0;
1633 exit_status = 0;
1634 }
1635
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001636 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001637 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001638
1639 /*
1640 * In interactive mode (with pseudo tty) display a message indicating
1641 * that the connection has been closed.
1642 */
1643 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001644 snprintf(buf, sizeof buf,
1645 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001646 buffer_append(&stderr_buffer, buf, strlen(buf));
1647 }
1648
Damien Miller95def091999-11-25 00:26:21 +11001649 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001650 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001651 len = atomicio(vwrite, fileno(stdout),
1652 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001653 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001654 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001655 else
1656 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001657 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658
Damien Miller95def091999-11-25 00:26:21 +11001659 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001660 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001661 len = atomicio(vwrite, fileno(stderr),
1662 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001663 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001664 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001665 else
1666 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001667 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668
Damien Miller95def091999-11-25 00:26:21 +11001669 /* Clear and free any buffers. */
1670 memset(buf, 0, sizeof(buf));
1671 buffer_free(&stdin_buffer);
1672 buffer_free(&stdout_buffer);
1673 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001674
Damien Miller95def091999-11-25 00:26:21 +11001675 /* Report bytes transferred, and transfer rates. */
1676 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001677 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1678 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1679 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001680 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001681 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001682 verbose("Bytes per second: sent %.1f, received %.1f",
1683 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001684 /* Return the exit status of the program. */
1685 debug("Exit status %d", exit_status);
1686 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001687}
Damien Millerb38eff82000-04-01 11:09:21 +10001688
1689/*********/
1690
Ben Lindstrombba81212001-06-25 05:01:22 +00001691static void
Damien Miller630d6f42002-01-22 23:17:30 +11001692client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001693{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001694 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001695 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001696 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001697 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001698 memset(data, 0, data_len);
1699 xfree(data);
1700}
Ben Lindstrombba81212001-06-25 05:01:22 +00001701static void
Damien Miller630d6f42002-01-22 23:17:30 +11001702client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001703{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001704 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001705 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001706 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001707 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001708 memset(data, 0, data_len);
1709 xfree(data);
1710}
Ben Lindstrombba81212001-06-25 05:01:22 +00001711static void
Damien Miller630d6f42002-01-22 23:17:30 +11001712client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001713{
Damien Millerb38eff82000-04-01 11:09:21 +10001714 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001715 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001716 /* Acknowledge the exit. */
1717 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1718 packet_send();
1719 /*
1720 * Must wait for packet to be sent since we are
1721 * exiting the loop.
1722 */
1723 packet_write_wait();
1724 /* Flag that we want to exit. */
1725 quit_pending = 1;
1726}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001727static void
1728client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1729{
1730 Channel *c = NULL;
1731 int remote_id, sock;
1732
1733 /* Read the remote channel number from the message. */
1734 remote_id = packet_get_int();
1735 packet_check_eom();
1736
1737 /*
1738 * Get a connection to the local authentication agent (this may again
1739 * get forwarded).
1740 */
1741 sock = ssh_get_authentication_socket();
1742
1743 /*
1744 * If we could not connect the agent, send an error message back to
1745 * the server. This should never happen unless the agent dies,
1746 * because authentication forwarding is only enabled if we have an
1747 * agent.
1748 */
1749 if (sock >= 0) {
1750 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1751 -1, 0, 0, 0, "authentication agent connection", 1);
1752 c->remote_id = remote_id;
1753 c->force_drain = 1;
1754 }
1755 if (c == NULL) {
1756 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1757 packet_put_int(remote_id);
1758 } else {
1759 /* Send a confirmation to the remote host. */
1760 debug("Forwarding authentication connection.");
1761 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1762 packet_put_int(remote_id);
1763 packet_put_int(c->self);
1764 }
1765 packet_send();
1766}
Damien Millerb38eff82000-04-01 11:09:21 +10001767
Ben Lindstrombba81212001-06-25 05:01:22 +00001768static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001769client_request_forwarded_tcpip(const char *request_type, int rchan)
1770{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001771 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001773 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774
1775 /* Get rest of the packet */
1776 listen_address = packet_get_string(NULL);
1777 listen_port = packet_get_int();
1778 originator_address = packet_get_string(NULL);
1779 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001780 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001781
Damien Millerbd740252008-05-19 15:37:09 +10001782 debug("client_request_forwarded_tcpip: listen %s port %d, "
1783 "originator %s port %d", listen_address, listen_port,
1784 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001785
Damien Millerbd740252008-05-19 15:37:09 +10001786 c = channel_connect_by_listen_address(listen_port,
1787 "forwarded-tcpip", originator_address);
1788
Damien Miller0bc1bd82000-11-13 22:57:25 +11001789 xfree(originator_address);
1790 xfree(listen_address);
1791 return c;
1792}
1793
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001794static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001795client_request_x11(const char *request_type, int rchan)
1796{
1797 Channel *c = NULL;
1798 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001799 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001800 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001801
1802 if (!options.forward_x11) {
1803 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001804 error("Warning: this is probably a break-in attempt by a "
1805 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001806 return NULL;
1807 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001808 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1809 verbose("Rejected X11 connection after ForwardX11Timeout "
1810 "expired");
1811 return NULL;
1812 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001813 originator = packet_get_string(NULL);
1814 if (datafellows & SSH_BUG_X11FWD) {
1815 debug2("buggy server: x11 request w/o originator_port");
1816 originator_port = 0;
1817 } else {
1818 originator_port = packet_get_int();
1819 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001820 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001821 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001822 debug("client_request_x11: request from %s %d", originator,
1823 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001824 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001825 sock = x11_connect_display();
1826 if (sock < 0)
1827 return NULL;
1828 c = channel_new("x11",
1829 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001830 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001831 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001832 return c;
1833}
1834
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001835static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001836client_request_agent(const char *request_type, int rchan)
1837{
1838 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001839 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001840
1841 if (!options.forward_agent) {
1842 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001843 error("Warning: this is probably a break-in attempt by a "
1844 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001845 return NULL;
1846 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001847 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001848 if (sock < 0)
1849 return NULL;
1850 c = channel_new("authentication agent connection",
1851 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001852 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001853 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001854 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001855 return c;
1856}
1857
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001858int
1859client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1860{
1861 Channel *c;
1862 int fd;
1863
1864 if (tun_mode == SSH_TUNMODE_NO)
1865 return 0;
1866
1867 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001868 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001869 return -1;
1870 }
1871
1872 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1873
1874 /* Open local tunnel device */
1875 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1876 error("Tunnel device open failed.");
1877 return -1;
1878 }
1879
1880 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1881 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1882 c->datagram = 1;
1883
1884#if defined(SSH_TUN_FILTER)
1885 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1886 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001887 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001888#endif
1889
1890 packet_start(SSH2_MSG_CHANNEL_OPEN);
1891 packet_put_cstring("tun@openssh.com");
1892 packet_put_int(c->self);
1893 packet_put_int(c->local_window_max);
1894 packet_put_int(c->local_maxpacket);
1895 packet_put_int(tun_mode);
1896 packet_put_int(remote_tun);
1897 packet_send();
1898
1899 return 0;
1900}
1901
Damien Millerbd483e72000-04-30 10:00:53 +10001902/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001903static void
Damien Miller630d6f42002-01-22 23:17:30 +11001904client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001905{
1906 Channel *c = NULL;
1907 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001908 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001909 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001910
1911 ctype = packet_get_string(&len);
1912 rchan = packet_get_int();
1913 rwindow = packet_get_int();
1914 rmaxpack = packet_get_int();
1915
Damien Millere247cc42000-05-07 12:03:14 +10001916 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001917 ctype, rchan, rwindow, rmaxpack);
1918
Damien Miller0bc1bd82000-11-13 22:57:25 +11001919 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1920 c = client_request_forwarded_tcpip(ctype, rchan);
1921 } else if (strcmp(ctype, "x11") == 0) {
1922 c = client_request_x11(ctype, rchan);
1923 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1924 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001925 }
1926/* XXX duplicate : */
1927 if (c != NULL) {
1928 debug("confirm %s", ctype);
1929 c->remote_id = rchan;
1930 c->remote_window = rwindow;
1931 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001932 if (c->type != SSH_CHANNEL_CONNECTING) {
1933 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1934 packet_put_int(c->remote_id);
1935 packet_put_int(c->self);
1936 packet_put_int(c->local_window);
1937 packet_put_int(c->local_maxpacket);
1938 packet_send();
1939 }
Damien Millerbd483e72000-04-30 10:00:53 +10001940 } else {
1941 debug("failure %s", ctype);
1942 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1943 packet_put_int(rchan);
1944 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001945 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001946 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001947 packet_put_cstring("");
1948 }
Damien Millerbd483e72000-04-30 10:00:53 +10001949 packet_send();
1950 }
1951 xfree(ctype);
1952}
Ben Lindstrombba81212001-06-25 05:01:22 +00001953static void
Damien Miller630d6f42002-01-22 23:17:30 +11001954client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001955{
1956 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001957 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001958 char *rtype;
1959
1960 id = packet_get_int();
1961 rtype = packet_get_string(NULL);
1962 reply = packet_get_char();
1963
1964 debug("client_input_channel_req: channel %d rtype %s reply %d",
1965 id, rtype, reply);
1966
Damien Miller3bbd8782004-06-18 22:23:22 +10001967 if (id == -1) {
1968 error("client_input_channel_req: request for channel -1");
1969 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001970 error("client_input_channel_req: channel %d: "
1971 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001972 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1973 packet_check_eom();
1974 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001975 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001976 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001977 if (c->ctl_chan != -1) {
1978 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001979 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001980 } else if (id == session_ident) {
1981 /* Record exit value of local session */
1982 success = 1;
1983 exit_status = exitval;
1984 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001985 /* Probably for a mux channel that has already closed */
1986 debug("%s: no sink for exit-status on channel %d",
1987 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001988 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001989 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001990 }
Damien Millerd0fdd682010-12-01 12:02:14 +11001991 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001992 packet_start(success ?
1993 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001994 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001995 packet_send();
1996 }
1997 xfree(rtype);
1998}
Damien Millerc3fa4072002-01-22 23:21:58 +11001999static void
2000client_input_global_request(int type, u_int32_t seq, void *ctxt)
2001{
2002 char *rtype;
2003 int want_reply;
2004 int success = 0;
2005
2006 rtype = packet_get_string(NULL);
2007 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002008 debug("client_input_global_request: rtype %s want_reply %d",
2009 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002010 if (want_reply) {
2011 packet_start(success ?
2012 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2013 packet_send();
2014 packet_write_wait();
2015 }
2016 xfree(rtype);
2017}
Damien Millerbd483e72000-04-30 10:00:53 +10002018
Damien Miller0e220db2004-06-15 10:34:08 +10002019void
Darren Tuckerfc959702004-07-17 16:12:08 +10002020client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002021 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002022{
2023 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002024 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002025
2026 debug2("%s: id %d", __func__, id);
2027
Darren Tucker5d78de62004-11-05 20:35:44 +11002028 if ((c = channel_lookup(id)) == NULL)
2029 fatal("client_session2_setup: channel %d: unknown channel", id);
2030
Damien Miller0dac6fb2010-11-20 15:19:38 +11002031 packet_set_interactive(want_tty,
2032 options.ip_qos_interactive, options.ip_qos_bulk);
2033
Damien Miller0e220db2004-06-15 10:34:08 +10002034 if (want_tty) {
2035 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002036
2037 /* Store window size in the packet. */
2038 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2039 memset(&ws, 0, sizeof(ws));
2040
Damien Miller5771ed72008-05-19 15:35:33 +10002041 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002042 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002043 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002044 packet_put_int((u_int)ws.ws_col);
2045 packet_put_int((u_int)ws.ws_row);
2046 packet_put_int((u_int)ws.ws_xpixel);
2047 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002048 if (tiop == NULL)
2049 tiop = get_saved_tio();
2050 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002051 packet_send();
2052 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002053 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002054 }
2055
2056 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002057 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002058 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002059 char *name, *val;
2060
2061 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002062 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002063 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002064 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002065 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002066 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002067 continue;
2068 }
2069 *val++ = '\0';
2070
2071 matched = 0;
2072 for (j = 0; j < options.num_send_env; j++) {
2073 if (match_pattern(name, options.send_env[j])) {
2074 matched = 1;
2075 break;
2076 }
2077 }
2078 if (!matched) {
2079 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002080 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002081 continue;
2082 }
2083
2084 debug("Sending env %s = %s", name, val);
2085 channel_request_start(id, "env", 0);
2086 packet_put_cstring(name);
2087 packet_put_cstring(val);
2088 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002089 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002090 }
2091 }
2092
2093 len = buffer_len(cmd);
2094 if (len > 0) {
2095 if (len > 900)
2096 len = 900;
2097 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002098 debug("Sending subsystem: %.*s",
2099 len, (u_char*)buffer_ptr(cmd));
2100 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002101 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002102 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002103 debug("Sending command: %.*s",
2104 len, (u_char*)buffer_ptr(cmd));
2105 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002106 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002107 }
2108 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2109 packet_send();
2110 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002111 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002112 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002113 packet_send();
2114 }
2115}
2116
Ben Lindstrombba81212001-06-25 05:01:22 +00002117static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002118client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002119{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002120 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002121
Damien Miller1383bd82000-04-06 12:32:37 +10002122 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2123 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2124 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2125 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002126 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002127 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2128 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002129 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002130 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002131 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2132 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002133 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002134
2135 /* rekeying */
2136 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002137
2138 /* global request reply messages */
2139 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2140 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002141}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002142
Ben Lindstrombba81212001-06-25 05:01:22 +00002143static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002144client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002145{
2146 dispatch_init(NULL);
2147 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2148 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2149 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002150 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2151 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2152 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002153 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2154 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2155 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002156
2157 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002158 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002159 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2160 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002161}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002162
Ben Lindstrombba81212001-06-25 05:01:22 +00002163static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002164client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002165{
2166 client_init_dispatch_13();
2167 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2168 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2169}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002170
Ben Lindstromdb47f382001-07-04 05:10:27 +00002171static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002172client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002173{
Damien Miller1383bd82000-04-06 12:32:37 +10002174 if (compat20)
2175 client_init_dispatch_20();
2176 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002177 client_init_dispatch_13();
2178 else
2179 client_init_dispatch_15();
2180}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002181
Damien Miller6c3eec72011-05-05 14:16:22 +10002182void
2183client_stop_mux(void)
2184{
2185 if (options.control_path != NULL && muxserver_sock != -1)
2186 unlink(options.control_path);
2187 /*
2188 * If we are in persist mode, signal that we should close when all
2189 * active channels are closed.
2190 */
Damien Miller4ac99c32011-06-20 14:43:31 +10002191 if (options.control_persist) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002192 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002193 setproctitle("[stopped mux]");
2194 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002195}
2196
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002197/* client specific fatal cleanup */
2198void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002199cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002200{
Damien Miller21771e22011-05-15 08:45:50 +10002201 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002202 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002203 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002204 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002205 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002206 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002207}