blob: b45e7298a802ff2e8aefb2bfae549cb214be7a4e [file] [log] [blame]
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001/* $OpenBSD: clientloop.c,v 1.195 2008/06/12 03:40:52 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
103#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000104#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100105#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "authfd.h"
107#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000108#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000109#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "monitor_fdpass.h"
111#include "match.h"
112#include "msg.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 Millerb1cbfa22008-05-19 16:00:08 +1000124extern int muxserver_sock;
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
145/* Common data for the client loop code. */
Damien Miller97c91f62006-04-23 12:08:37 +1000146static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000147static int escape_char1; /* Escape character. (proto1 only) */
148static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100149static int last_was_cr; /* Last character was a newline. */
150static int exit_status; /* Used to store the exit status of the command. */
151static int stdin_eof; /* EOF has been encountered on standard error. */
152static Buffer stdin_buffer; /* Buffer for stdin data. */
153static Buffer stdout_buffer; /* Buffer for stdout data. */
154static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155static u_long stdin_bytes, stdout_bytes, stderr_bytes;
156static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100157static int connection_in; /* Connection to server (input). */
158static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000159static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000160static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000163int session_ident = -1;
164
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000165/* Track escape per proto2 channel */
166struct escape_filter_ctx {
167 int escape_pending;
168 int escape_char;
169};
170
171/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000172struct channel_reply_ctx {
173 const char *request_type;
174 int id, do_close;
175};
176
Ben Lindstromf28f6342001-04-04 02:03:04 +0000177/*XXX*/
178extern Kex *xxx_kex;
179
Damien Miller0e220db2004-06-15 10:34:08 +1000180void ssh_process_session2_setup(int, int, int, Buffer *);
181
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182/* Restores stdin to blocking mode. */
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000185leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186{
Damien Miller95def091999-11-25 00:26:21 +1100187 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000188 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100189 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100190 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Miller95def091999-11-25 00:26:21 +1100193/* Puts stdin terminal in non-blocking mode. */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000196enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller95def091999-11-25 00:26:21 +1100198 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000199 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200}
201
Damien Miller5428f641999-11-25 11:54:57 +1100202/*
203 * Signal handler for the window change signal (SIGWINCH). This just sets a
204 * flag indicating that the window has changed.
205 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100206/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static void
Damien Miller95def091999-11-25 00:26:21 +1100208window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209{
Damien Miller95def091999-11-25 00:26:21 +1100210 received_window_change_signal = 1;
211 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212}
213
Damien Miller5428f641999-11-25 11:54:57 +1100214/*
215 * Signal handler for signals that cause the program to terminate. These
216 * signals must be trapped to restore terminal modes.
217 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100218/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static void
Damien Miller95def091999-11-25 00:26:21 +1100220signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000222 received_signal = sig;
223 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
227 * Returns current time in seconds from Jan 1, 1970 with the maximum
228 * available resolution.
229 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000232get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233{
Damien Miller95def091999-11-25 00:26:21 +1100234 struct timeval tv;
235 gettimeofday(&tv, NULL);
236 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237}
238
Damien Miller17e7ed02005-06-17 12:54:33 +1000239#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
240void
241client_x11_get_proto(const char *display, const char *xauth_path,
242 u_int trusted, char **_proto, char **_data)
243{
244 char cmd[1024];
245 char line[512];
246 char xdisplay[512];
247 static char proto[512], data[512];
248 FILE *f;
249 int got_data = 0, generated = 0, do_unlink = 0, i;
250 char *xauthdir, *xauthfile;
251 struct stat st;
252
253 xauthdir = xauthfile = NULL;
254 *_proto = proto;
255 *_data = data;
256 proto[0] = data[0] = '\0';
257
258 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
259 debug("No xauth program.");
260 } else {
261 if (display == NULL) {
262 debug("x11_get_proto: DISPLAY not set");
263 return;
264 }
265 /*
266 * Handle FamilyLocal case where $DISPLAY does
267 * not match an authorization entry. For this we
268 * just try "xauth list unix:displaynum.screennum".
269 * XXX: "localhost" match to determine FamilyLocal
270 * is not perfect.
271 */
272 if (strncmp(display, "localhost:", 10) == 0) {
273 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
274 display + 10);
275 display = xdisplay;
276 }
277 if (trusted == 0) {
278 xauthdir = xmalloc(MAXPATHLEN);
279 xauthfile = xmalloc(MAXPATHLEN);
280 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
281 if (mkdtemp(xauthdir) != NULL) {
282 do_unlink = 1;
283 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
284 xauthdir);
285 snprintf(cmd, sizeof(cmd),
286 "%s -f %s generate %s " SSH_X11_PROTO
287 " untrusted timeout 1200 2>" _PATH_DEVNULL,
288 xauth_path, xauthfile, display);
289 debug2("x11_get_proto: %s", cmd);
290 if (system(cmd) == 0)
291 generated = 1;
292 }
293 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000294
295 /*
296 * When in untrusted mode, we read the cookie only if it was
297 * successfully generated as an untrusted one in the step
298 * above.
299 */
300 if (trusted || generated) {
301 snprintf(cmd, sizeof(cmd),
302 "%s %s%s list %s 2>" _PATH_DEVNULL,
303 xauth_path,
304 generated ? "-f " : "" ,
305 generated ? xauthfile : "",
306 display);
307 debug2("x11_get_proto: %s", cmd);
308 f = popen(cmd, "r");
309 if (f && fgets(line, sizeof(line), f) &&
310 sscanf(line, "%*s %511s %511s", proto, data) == 2)
311 got_data = 1;
312 if (f)
313 pclose(f);
314 } else
315 error("Warning: untrusted X11 forwarding setup failed: "
316 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000317 }
318
319 if (do_unlink) {
320 unlink(xauthfile);
321 rmdir(xauthdir);
322 }
323 if (xauthdir)
324 xfree(xauthdir);
325 if (xauthfile)
326 xfree(xauthfile);
327
328 /*
329 * If we didn't get authentication data, just make up some
330 * data. The forwarding code will check the validity of the
331 * response anyway, and substitute this data. The X11
332 * server, however, will ignore this fake data and use
333 * whatever authentication mechanisms it was using otherwise
334 * for the local connection.
335 */
336 if (!got_data) {
337 u_int32_t rnd = 0;
338
339 logit("Warning: No xauth data; "
340 "using fake authentication data for X11 forwarding.");
341 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
342 for (i = 0; i < 16; i++) {
343 if (i % 4 == 0)
344 rnd = arc4random();
345 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
346 rnd & 0xff);
347 rnd >>= 8;
348 }
349 }
350}
351
Damien Miller5428f641999-11-25 11:54:57 +1100352/*
353 * This is called when the interactive is entered. This checks if there is
354 * an EOF coming on stdin. We must check this explicitly, as select() does
355 * not appear to wake up when redirecting from /dev/null.
356 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Ben Lindstrombba81212001-06-25 05:01:22 +0000358static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000359client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Damien Miller95def091999-11-25 00:26:21 +1100361 int len;
362 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Miller5428f641999-11-25 11:54:57 +1100364 /*
365 * If standard input is to be "redirected from /dev/null", we simply
366 * mark that we have seen an EOF and send an EOF message to the
367 * server. Otherwise, we try to read a single character; it appears
368 * that for some files, such /dev/null, select() never wakes up for
369 * read for this descriptor, which means that we never get EOF. This
370 * way we will get the EOF if stdin comes from /dev/null or similar.
371 */
Damien Miller95def091999-11-25 00:26:21 +1100372 if (stdin_null_flag) {
373 /* Fake EOF on stdin. */
374 debug("Sending eof.");
375 stdin_eof = 1;
376 packet_start(SSH_CMSG_EOF);
377 packet_send();
378 } else {
Damien Miller95def091999-11-25 00:26:21 +1100379 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
Damien Miller95def091999-11-25 00:26:21 +1100381 /* Check for immediate EOF on stdin. */
382 len = read(fileno(stdin), buf, 1);
383 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100384 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100385 debug("Sending eof.");
386 stdin_eof = 1;
387 packet_start(SSH_CMSG_EOF);
388 packet_send();
389 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100390 /*
391 * Got data. We must store the data in the buffer,
392 * and also process it as an escape character if
393 * appropriate.
394 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000395 if ((u_char) buf[0] == escape_char1)
396 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000397 else
Damien Miller95def091999-11-25 00:26:21 +1100398 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100399 }
Damien Miller95def091999-11-25 00:26:21 +1100400 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller5428f641999-11-25 11:54:57 +1100405/*
406 * Make packets from buffered stdin data, and buffer them for sending to the
407 * connection.
408 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000411client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000413 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Send buffered stdin data to the server. */
416 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100417 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100418 len = buffer_len(&stdin_buffer);
419 /* Keep the packets at reasonable size. */
420 if (len > packet_get_maxsize())
421 len = packet_get_maxsize();
422 packet_start(SSH_CMSG_STDIN_DATA);
423 packet_put_string(buffer_ptr(&stdin_buffer), len);
424 packet_send();
425 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000426 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100427 /* If we have a pending EOF, send it now. */
428 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
429 packet_start(SSH_CMSG_EOF);
430 packet_send();
431 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
434
Damien Miller5428f641999-11-25 11:54:57 +1100435/*
436 * Checks if the client window has changed, and sends a packet about it to
437 * the server if so. The actual change is detected elsewhere (by a software
438 * interrupt on Unix); this just checks the flag and sends a message if
439 * appropriate.
440 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Ben Lindstrombba81212001-06-25 05:01:22 +0000442static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000443client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444{
Damien Miller1383bd82000-04-06 12:32:37 +1000445 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller1383bd82000-04-06 12:32:37 +1000447 if (! received_window_change_signal)
448 return;
449 /** XXX race */
450 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Millerd3444942000-09-30 14:20:03 +1100452 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000453
454 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000455 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000456 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000457 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
458 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000459 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100460 packet_put_int((u_int)ws.ws_row);
461 packet_put_int((u_int)ws.ws_col);
462 packet_put_int((u_int)ws.ws_xpixel);
463 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000464 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}
467
Damien Miller509b0102003-12-17 16:33:10 +1100468static void
469client_global_request_reply(int type, u_int32_t seq, void *ctxt)
470{
Damien Miller58226f62008-03-07 18:33:30 +1100471 keep_alive_timeouts = 0;
Damien Miller509b0102003-12-17 16:33:10 +1100472 client_global_request_reply_fwd(type, seq, ctxt);
473}
474
475static void
476server_alive_check(void)
477{
Damien Miller58226f62008-03-07 18:33:30 +1100478 if (++keep_alive_timeouts > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000479 logit("Timeout, server not responding.");
480 cleanup_exit(255);
481 }
Damien Miller509b0102003-12-17 16:33:10 +1100482 packet_start(SSH2_MSG_GLOBAL_REQUEST);
483 packet_put_cstring("keepalive@openssh.com");
484 packet_put_char(1); /* boolean: want reply */
485 packet_send();
486}
487
Damien Miller5428f641999-11-25 11:54:57 +1100488/*
489 * Waits until the client can do something (some data becomes available on
490 * one of the file descriptors).
491 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000492static void
Damien Miller5e953212001-01-30 09:14:00 +1100493client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000494 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495{
Damien Miller509b0102003-12-17 16:33:10 +1100496 struct timeval tv, *tvp;
497 int ret;
498
Damien Miller5e953212001-01-30 09:14:00 +1100499 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000500 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Miller1383bd82000-04-06 12:32:37 +1000502 if (!compat20) {
503 /* Read from the connection, unless our buffers are full. */
504 if (buffer_len(&stdout_buffer) < buffer_high &&
505 buffer_len(&stderr_buffer) < buffer_high &&
506 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100507 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000508 /*
509 * Read from stdin, unless we have seen EOF or have very much
510 * buffered data to send to the server.
511 */
512 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100513 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000514
515 /* Select stdout/stderr if have data in buffer. */
516 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100517 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000518 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100519 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000520 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000521 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000522 if (session_closed && !channel_still_open() &&
523 !packet_have_data_to_write()) {
524 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100525 memset(*readsetp, 0, *nallocp);
526 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000527 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000528 } else {
529 FD_SET(connection_in, *readsetp);
530 }
Damien Miller1383bd82000-04-06 12:32:37 +1000531 }
532
Damien Miller95def091999-11-25 00:26:21 +1100533 /* Select server connection if have data to write to the server. */
534 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100535 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100536
Damien Millerb1cbfa22008-05-19 16:00:08 +1000537 if (muxserver_sock != -1)
538 FD_SET(muxserver_sock, *readsetp);
Damien Miller0e220db2004-06-15 10:34:08 +1000539
Damien Miller5428f641999-11-25 11:54:57 +1100540 /*
541 * Wait for something to happen. This will suspend the process until
542 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100543 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100544 */
Damien Miller95def091999-11-25 00:26:21 +1100545
Damien Miller509b0102003-12-17 16:33:10 +1100546 if (options.server_alive_interval == 0 || !compat20)
547 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000548 else {
Damien Miller509b0102003-12-17 16:33:10 +1100549 tv.tv_sec = options.server_alive_interval;
550 tv.tv_usec = 0;
551 tvp = &tv;
552 }
553 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
554 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100555 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000556
557 /*
558 * We have to clear the select masks, because we return.
559 * We have to return, because the mainloop checks for the flags
560 * set by the signal handlers.
561 */
Damien Miller79faeff2001-11-12 11:06:32 +1100562 memset(*readsetp, 0, *nallocp);
563 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000564
Damien Miller95def091999-11-25 00:26:21 +1100565 if (errno == EINTR)
566 return;
567 /* Note: we might still have data in the buffers. */
568 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
569 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100570 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100571 } else if (ret == 0)
572 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573}
574
Ben Lindstrombba81212001-06-25 05:01:22 +0000575static void
Damien Millerad833b32000-08-23 10:46:23 +1000576client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577{
Damien Miller95def091999-11-25 00:26:21 +1100578 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000579 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000580 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000581 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000582 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
Damien Miller95def091999-11-25 00:26:21 +1100584 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Damien Miller5428f641999-11-25 11:54:57 +1100586 /*
587 * Free (and clear) the buffer to reduce the amount of data that gets
588 * written to swap.
589 */
Damien Millerad833b32000-08-23 10:46:23 +1000590 buffer_free(bin);
591 buffer_free(bout);
592 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593
Damien Miller95def091999-11-25 00:26:21 +1100594 /* Send the suspend signal to the program itself. */
595 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Darren Tucker5d78de62004-11-05 20:35:44 +1100597 /* Reset window sizes in case they have changed */
598 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
Damien Miller95def091999-11-25 00:26:21 +1100600 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000601 buffer_init(bin);
602 buffer_init(bout);
603 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
Damien Miller95def091999-11-25 00:26:21 +1100605 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606}
607
Ben Lindstrombba81212001-06-25 05:01:22 +0000608static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100609client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610{
Damien Miller1383bd82000-04-06 12:32:37 +1000611 int len;
612 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller5428f641999-11-25 11:54:57 +1100614 /*
615 * Read input from the server, and add any such data to the buffer of
616 * the packet subsystem.
617 */
Damien Miller95def091999-11-25 00:26:21 +1100618 if (FD_ISSET(connection_in, readset)) {
619 /* Read as much as possible. */
620 len = read(connection_in, buf, sizeof(buf));
621 if (len == 0) {
622 /* Received EOF. The remote host has closed the connection. */
623 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
624 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626 quit_pending = 1;
627 return;
Damien Miller95def091999-11-25 00:26:21 +1100628 }
Damien Miller5428f641999-11-25 11:54:57 +1100629 /*
630 * There is a kernel bug on Solaris that causes select to
631 * sometimes wake up even though there is no data available.
632 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000633 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100634 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635
Damien Miller95def091999-11-25 00:26:21 +1100636 if (len < 0) {
637 /* An error has encountered. Perhaps there is a network problem. */
638 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
639 host, strerror(errno));
640 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100641 quit_pending = 1;
642 return;
643 }
644 packet_process_incoming(buf, len);
645 }
Damien Miller1383bd82000-04-06 12:32:37 +1000646}
647
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000648static void
Damien Miller5771ed72008-05-19 15:35:33 +1000649client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000650{
Damien Miller5771ed72008-05-19 15:35:33 +1000651 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
652 char errmsg[256];
653 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000654
Damien Miller5771ed72008-05-19 15:35:33 +1000655 /* XXX supress on mux _client_ quietmode */
656 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
657 c->ctl_fd != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000658
Damien Miller5771ed72008-05-19 15:35:33 +1000659 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
660 debug2("%s request accepted on channel %d",
661 cr->request_type, c->self);
662 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
663 if (tochan) {
664 snprintf(errmsg, sizeof(errmsg),
665 "%s request failed\r\n", cr->request_type);
666 } else {
667 snprintf(errmsg, sizeof(errmsg),
668 "%s request failed on channel %d",
669 cr->request_type, c->self);
670 }
671 /* If error occurred on primary session channel, then exit */
672 if (cr->do_close && c->self == session_ident)
673 fatal("%s", errmsg);
674 /* If error occurred on mux client, append to their stderr */
675 if (tochan)
676 buffer_append(&c->extended, errmsg, strlen(errmsg));
677 else
678 error("%s", errmsg);
679 if (cr->do_close) {
680 chan_read_failed(c);
681 chan_write_failed(c);
682 }
Damien Miller0e220db2004-06-15 10:34:08 +1000683 }
Damien Miller5771ed72008-05-19 15:35:33 +1000684 xfree(cr);
685}
Damien Miller0e220db2004-06-15 10:34:08 +1000686
Damien Miller5771ed72008-05-19 15:35:33 +1000687static void
688client_abandon_status_confirm(Channel *c, void *ctx)
689{
690 xfree(ctx);
691}
692
693static void
694client_expect_confirm(int id, const char *request, int do_close)
695{
696 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
697
698 cr->request_type = request;
699 cr->do_close = do_close;
700
701 channel_register_status_confirm(id, client_status_confirm,
702 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000703}
704
705static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000706process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000707{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000708 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100709 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000710 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000711 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100712 u_short cancel_port;
713 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000714
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100715 bzero(&fwd, sizeof(fwd));
716 fwd.listen_host = fwd.connect_host = NULL;
717
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000718 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000719 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000720 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000721 if (s == NULL)
722 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100723 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000724 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000725 if (*s == '-')
726 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000727 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000728 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000729
Darren Tucker1973c882004-05-24 10:34:36 +1000730 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000731 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000732 logit(" -L[bind_address:]port:host:hostport "
733 "Request local forward");
734 logit(" -R[bind_address:]port:host:hostport "
735 "Request remote forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000736 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000737 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100738 if (!options.permit_local_command)
739 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000740 logit(" !args "
741 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100742 goto out;
743 }
744
745 if (*s == '!' && options.permit_local_command) {
746 s++;
747 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000748 goto out;
749 }
750
751 if (*s == 'K') {
752 delete = 1;
753 s++;
754 }
755 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +1000756 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000757 goto out;
758 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000759 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000760 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +1000761 if (local && delete) {
762 logit("Not supported.");
763 goto out;
764 }
765 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000766 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000767 goto out;
768 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000769
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100770 while (isspace(*++s))
771 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000772
Darren Tuckere7066df2004-05-24 10:18:05 +1000773 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100774 cancel_port = 0;
775 cancel_host = hpdelim(&s); /* may be NULL */
776 if (s != NULL) {
777 cancel_port = a2port(s);
778 cancel_host = cleanhostname(cancel_host);
779 } else {
780 cancel_port = a2port(cancel_host);
781 cancel_host = NULL;
782 }
783 if (cancel_port == 0) {
784 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000785 goto out;
786 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100787 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000788 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100789 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000790 logit("Bad forwarding specification.");
791 goto out;
792 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000793 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100794 if (channel_setup_local_fwd_listener(fwd.listen_host,
795 fwd.listen_port, fwd.connect_host,
796 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000797 logit("Port forwarding failed.");
798 goto out;
799 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100800 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000801 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100802 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000803 fwd.connect_port) < 0) {
804 logit("Port forwarding failed.");
805 goto out;
806 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100807 }
808
Darren Tuckere7066df2004-05-24 10:18:05 +1000809 logit("Forwarding port.");
810 }
811
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000812out:
813 signal(SIGINT, handler);
814 enter_raw_mode();
815 if (cmd)
816 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100817 if (fwd.listen_host != NULL)
818 xfree(fwd.listen_host);
819 if (fwd.connect_host != NULL)
820 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000821}
822
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000823/*
824 * Process the characters one by one, call with c==NULL for proto1 case.
825 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000826static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000827process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
828 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000829{
830 char string[1024];
831 pid_t pid;
832 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000833 u_int i;
834 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000835 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000836 int *escape_pendingp, escape_char;
837 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000838
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000839 if (c == NULL) {
840 escape_pendingp = &escape_pending1;
841 escape_char = escape_char1;
842 } else {
843 if (c->filter_ctx == NULL)
844 return 0;
845 efc = (struct escape_filter_ctx *)c->filter_ctx;
846 escape_pendingp = &efc->escape_pending;
847 escape_char = efc->escape_char;
848 }
849
Damien Millereccb9de2005-06-17 12:59:34 +1000850 if (len <= 0)
851 return (0);
852
853 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000854 /* Get one character at a time. */
855 ch = buf[i];
856
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000857 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000858 /* We have previously seen an escape character. */
859 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000860 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000861
862 /* Process the escaped character. */
863 switch (ch) {
864 case '.':
865 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000866 snprintf(string, sizeof string, "%c.\r\n",
867 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000868 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000869
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000870 if (c && c->ctl_fd != -1) {
871 chan_read_failed(c);
872 chan_write_failed(c);
873 return 0;
874 } else
875 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000876 return -1;
877
878 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000879 /* XXX support this for mux clients */
880 if (c && c->ctl_fd != -1) {
881 noescape:
882 snprintf(string, sizeof string,
883 "%c%c escape not available to "
884 "multiplexed sessions\r\n",
885 escape_char, ch);
886 buffer_append(berr, string,
887 strlen(string));
888 continue;
889 }
Damien Millerad833b32000-08-23 10:46:23 +1000890 /* Suspend the program. */
891 /* Print a message to that effect to the user. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000892 snprintf(string, sizeof string,
893 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000894 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000895
896 /* Restore terminal modes and suspend. */
897 client_suspend_self(bin, bout, berr);
898
899 /* We have been continued. */
900 continue;
901
Damien Miller54c45982003-05-15 10:20:13 +1000902 case 'B':
903 if (compat20) {
904 snprintf(string, sizeof string,
905 "%cB\r\n", escape_char);
906 buffer_append(berr, string,
907 strlen(string));
908 channel_request_start(session_ident,
909 "break", 0);
910 packet_put_int(1000);
911 packet_send();
912 }
913 continue;
914
Ben Lindstromf28f6342001-04-04 02:03:04 +0000915 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000916 if (compat20) {
917 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +1000918 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000919 else
920 need_rekeying = 1;
921 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000922 continue;
923
Damien Millerad833b32000-08-23 10:46:23 +1000924 case '&':
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000925 if (c && c->ctl_fd != -1)
926 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +1000927 /*
928 * Detach the program (continue to serve connections,
929 * but put in background and no more new connections).
930 */
Damien Miller96507ef2001-11-12 10:52:25 +1100931 /* Restore tty modes. */
932 leave_raw_mode();
933
934 /* Stop listening for new connections. */
935 channel_stop_listening();
936
937 snprintf(string, sizeof string,
938 "%c& [backgrounded]\n", escape_char);
939 buffer_append(berr, string, strlen(string));
940
941 /* Fork into background. */
942 pid = fork();
943 if (pid < 0) {
944 error("fork: %.100s", strerror(errno));
945 continue;
946 }
947 if (pid != 0) { /* This is the parent. */
948 /* The parent just exits. */
949 exit(0);
950 }
951 /* The child continues serving connections. */
952 if (compat20) {
953 buffer_append(bin, "\004", 1);
954 /* fake EOF on stdin */
955 return -1;
956 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +1000957 /*
958 * Sending SSH_CMSG_EOF alone does not always appear
959 * to be enough. So we try to send an EOF character
960 * first.
961 */
962 packet_start(SSH_CMSG_STDIN_DATA);
963 packet_put_string("\004", 1);
964 packet_send();
965 /* Close stdin. */
966 stdin_eof = 1;
967 if (buffer_len(bin) == 0) {
968 packet_start(SSH_CMSG_EOF);
969 packet_send();
970 }
971 }
Damien Miller96507ef2001-11-12 10:52:25 +1100972 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000973
974 case '?':
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000975 if (c && c->ctl_fd != -1) {
976 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +1000977"%c?\r\n\
978Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000979 %c. - terminate session\r\n\
980 %cB - send a BREAK to the remote system\r\n\
981 %cC - open a command line\r\n\
982 %cR - Request rekey (SSH protocol 2 only)\r\n\
983 %c# - list forwarded connections\r\n\
984 %c? - this message\r\n\
985 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000986(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000987 escape_char, escape_char,
988 escape_char, escape_char,
989 escape_char, escape_char,
990 escape_char, escape_char,
991 escape_char);
992 } else {
993 snprintf(string, sizeof string,
994"%c?\r\n\
995Supported escape sequences:\r\n\
996 %c. - terminate connection (and any multiplexed sessions)\r\n\
997 %cB - send a BREAK to the remote system\r\n\
998 %cC - open a command line\r\n\
999 %cR - Request rekey (SSH protocol 2 only)\r\n\
1000 %c^Z - suspend ssh\r\n\
1001 %c# - list forwarded connections\r\n\
1002 %c& - background ssh (when waiting for connections to terminate)\r\n\
1003 %c? - this message\r\n\
1004 %c%c - send the escape character by typing it twice\r\n\
1005(Note that escapes are only recognized immediately after newline.)\r\n",
1006 escape_char, escape_char,
1007 escape_char, escape_char,
1008 escape_char, escape_char,
1009 escape_char, escape_char,
1010 escape_char, escape_char,
1011 escape_char);
1012 }
Damien Millerad833b32000-08-23 10:46:23 +10001013 buffer_append(berr, string, strlen(string));
1014 continue;
1015
1016 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001017 snprintf(string, sizeof string, "%c#\r\n",
1018 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001019 buffer_append(berr, string, strlen(string));
1020 s = channel_open_message();
1021 buffer_append(berr, s, strlen(s));
1022 xfree(s);
1023 continue;
1024
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001025 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001026 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001027 continue;
1028
Damien Millerad833b32000-08-23 10:46:23 +10001029 default:
1030 if (ch != escape_char) {
1031 buffer_put_char(bin, escape_char);
1032 bytes++;
1033 }
1034 /* Escaped characters fall through here */
1035 break;
1036 }
1037 } else {
1038 /*
1039 * The previous character was not an escape char. Check if this
1040 * is an escape.
1041 */
1042 if (last_was_cr && ch == escape_char) {
1043 /* It is. Set the flag and continue to next character. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001044 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001045 continue;
1046 }
1047 }
1048
1049 /*
1050 * Normal character. Record whether it was a newline,
1051 * and append it to the buffer.
1052 */
1053 last_was_cr = (ch == '\r' || ch == '\n');
1054 buffer_put_char(bin, ch);
1055 bytes++;
1056 }
1057 return bytes;
1058}
1059
Ben Lindstrombba81212001-06-25 05:01:22 +00001060static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001061client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001062{
Damien Miller166fca82000-04-20 07:42:21 +10001063 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001064 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 /* Read input from stdin. */
1067 if (FD_ISSET(fileno(stdin), readset)) {
1068 /* Read as much as possible. */
1069 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001070 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1071 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001072 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001073 /*
1074 * Received EOF or error. They are treated
1075 * similarly, except that an error message is printed
1076 * if it was an error condition.
1077 */
Damien Miller95def091999-11-25 00:26:21 +11001078 if (len < 0) {
1079 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1080 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001081 }
1082 /* Mark that we have seen EOF. */
1083 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001084 /*
1085 * Send an EOF message to the server unless there is
1086 * data in the buffer. If there is data in the
1087 * buffer, no message will be sent now. Code
1088 * elsewhere will send the EOF when the buffer
1089 * becomes empty if stdin_eof is set.
1090 */
Damien Miller95def091999-11-25 00:26:21 +11001091 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092 packet_start(SSH_CMSG_EOF);
1093 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001094 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001095 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001096 /*
1097 * Normal successful read, and no escape character.
1098 * Just append the data to buffer.
1099 */
Damien Miller95def091999-11-25 00:26:21 +11001100 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001101 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001102 /*
1103 * Normal, successful read. But we have an escape character
1104 * and have to process the characters one by one.
1105 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001106 if (process_escapes(NULL, &stdin_buffer,
1107 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001108 return;
Damien Miller95def091999-11-25 00:26:21 +11001109 }
1110 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111}
1112
Ben Lindstrombba81212001-06-25 05:01:22 +00001113static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001114client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115{
Damien Miller95def091999-11-25 00:26:21 +11001116 int len;
1117 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118
Damien Miller95def091999-11-25 00:26:21 +11001119 /* Write buffered output to stdout. */
1120 if (FD_ISSET(fileno(stdout), writeset)) {
1121 /* Write as much data as possible. */
1122 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001123 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001124 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001125 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001126 len = 0;
1127 else {
Damien Miller5428f641999-11-25 11:54:57 +11001128 /*
1129 * An error or EOF was encountered. Put an
1130 * error message to stderr buffer.
1131 */
Damien Miller95def091999-11-25 00:26:21 +11001132 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1133 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001134 quit_pending = 1;
1135 return;
1136 }
1137 }
1138 /* Consume printed data from the buffer. */
1139 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001140 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141 }
Damien Miller95def091999-11-25 00:26:21 +11001142 /* Write buffered output to stderr. */
1143 if (FD_ISSET(fileno(stderr), writeset)) {
1144 /* Write as much data as possible. */
1145 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001146 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001147 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001148 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001149 len = 0;
1150 else {
Damien Miller5428f641999-11-25 11:54:57 +11001151 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001152 quit_pending = 1;
1153 return;
1154 }
1155 }
1156 /* Consume printed characters from the buffer. */
1157 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001158 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001159 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160}
1161
Damien Miller5428f641999-11-25 11:54:57 +11001162/*
Damien Millerb38eff82000-04-01 11:09:21 +10001163 * Get packets from the connection input buffer, and process them as long as
1164 * there are packets available.
1165 *
1166 * Any unknown packets received during the actual
1167 * session cause the session to terminate. This is
1168 * intended to make debugging easier since no
1169 * confirmations are sent. Any compatible protocol
1170 * extensions must be negotiated during the
1171 * preparatory phase.
1172 */
1173
Ben Lindstrombba81212001-06-25 05:01:22 +00001174static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001175client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001176{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001177 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001178}
1179
Damien Millerad833b32000-08-23 10:46:23 +10001180/* scan buf[] for '~' before sending data to the peer */
1181
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001182/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1183void *
1184client_new_escape_filter_ctx(int escape_char)
1185{
1186 struct escape_filter_ctx *ret;
1187
1188 ret = xmalloc(sizeof(*ret));
1189 ret->escape_pending = 0;
1190 ret->escape_char = escape_char;
1191 return (void *)ret;
1192}
1193
1194int
1195client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001196{
Damien Miller5771ed72008-05-19 15:35:33 +10001197 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1198 return 0;
1199
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001200 return process_escapes(c, &c->input, &c->output, &c->extended,
1201 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001202}
1203
Ben Lindstrombba81212001-06-25 05:01:22 +00001204static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001205client_channel_closed(int id, void *arg)
1206{
Damien Miller3ec27592001-10-12 11:35:04 +10001207 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001208 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001209 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001210}
1211
Damien Millerb38eff82000-04-01 11:09:21 +10001212/*
Damien Miller5428f641999-11-25 11:54:57 +11001213 * Implements the interactive session with the server. This is called after
1214 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001215 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1216 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001217 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218
Damien Miller4af51302000-04-16 11:18:38 +10001219int
Damien Millerad833b32000-08-23 10:46:23 +10001220client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221{
Damien Miller5e953212001-01-30 09:14:00 +11001222 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001223 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001224 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1225 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001226 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227
Damien Miller95def091999-11-25 00:26:21 +11001228 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229
Damien Miller95def091999-11-25 00:26:21 +11001230 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231
Damien Miller95def091999-11-25 00:26:21 +11001232 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001233 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001234 last_was_cr = 1;
1235 exit_status = -1;
1236 stdin_eof = 0;
1237 buffer_high = 64 * 1024;
1238 connection_in = packet_get_connection_in();
1239 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001240 max_fd = MAX(connection_in, connection_out);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001241 if (muxserver_sock != -1)
1242 max_fd = MAX(max_fd, muxserver_sock);
Damien Miller5e953212001-01-30 09:14:00 +11001243
1244 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001245 /* enable nonblocking unless tty */
1246 if (!isatty(fileno(stdin)))
1247 set_nonblock(fileno(stdin));
1248 if (!isatty(fileno(stdout)))
1249 set_nonblock(fileno(stdout));
1250 if (!isatty(fileno(stderr)))
1251 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001252 max_fd = MAX(max_fd, fileno(stdin));
1253 max_fd = MAX(max_fd, fileno(stdout));
1254 max_fd = MAX(max_fd, fileno(stderr));
1255 }
Damien Miller95def091999-11-25 00:26:21 +11001256 stdin_bytes = 0;
1257 stdout_bytes = 0;
1258 stderr_bytes = 0;
1259 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001260 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261
Damien Miller95def091999-11-25 00:26:21 +11001262 /* Initialize buffers. */
1263 buffer_init(&stdin_buffer);
1264 buffer_init(&stdout_buffer);
1265 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266
Damien Millerb38eff82000-04-01 11:09:21 +10001267 client_init_dispatch();
1268
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001269 /*
1270 * Set signal handlers, (e.g. to restore non-blocking mode)
1271 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1272 */
Darren Tucker07336da2004-11-05 20:02:16 +11001273 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1274 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001275 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1276 signal(SIGINT, signal_handler);
1277 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1278 signal(SIGQUIT, signal_handler);
1279 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1280 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001281 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282
Damien Miller95def091999-11-25 00:26:21 +11001283 if (have_pty)
1284 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285
Ben Lindstrom5b828322001-02-09 01:34:36 +00001286 if (compat20) {
1287 session_ident = ssh2_chan_id;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001288 if (escape_char_arg != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001289 channel_register_filter(session_ident,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001290 client_simple_escape_filter, NULL,
1291 client_new_escape_filter_ctx(escape_char_arg));
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001292 if (session_ident != -1)
1293 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001294 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001295 } else {
1296 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001297 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001298 }
Damien Millerad833b32000-08-23 10:46:23 +10001299
Damien Miller95def091999-11-25 00:26:21 +11001300 /* Main loop of the client for the interactive session mode. */
1301 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller5428f641999-11-25 11:54:57 +11001303 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001304 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001306 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001307 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001308
1309 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001310
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001311 if (rekeying) {
1312 debug("rekeying in progress");
1313 } else {
1314 /*
1315 * Make packets of buffered stdin data, and buffer
1316 * them for sending to the server.
1317 */
1318 if (!compat20)
1319 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001321 /*
1322 * Make packets from buffered channel data, and
1323 * enqueue them for sending to the server.
1324 */
1325 if (packet_not_very_much_data_to_write())
1326 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001328 /*
1329 * Check if the window size has changed, and buffer a
1330 * message about it to the server if so.
1331 */
1332 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001334 if (quit_pending)
1335 break;
1336 }
Damien Miller5428f641999-11-25 11:54:57 +11001337 /*
1338 * Wait until we have something to do (something becomes
1339 * available on one of the descriptors).
1340 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001341 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001342 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001343 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344
Damien Miller95def091999-11-25 00:26:21 +11001345 if (quit_pending)
1346 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001348 /* Do channel operations unless rekeying in progress. */
1349 if (!rekeying) {
1350 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001351 if (need_rekeying || packet_need_rekeying()) {
1352 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001353 xxx_kex->done = 0;
1354 kex_send_kexinit(xxx_kex);
1355 need_rekeying = 0;
1356 }
1357 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Damien Miller1383bd82000-04-06 12:32:37 +10001359 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001360 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Damien Miller0e220db2004-06-15 10:34:08 +10001362 /* Accept control connections. */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001363 if (muxserver_sock != -1 &&FD_ISSET(muxserver_sock, readset)) {
1364 if (muxserver_accept_control())
1365 quit_pending = 1;
1366 }
Damien Miller0e220db2004-06-15 10:34:08 +10001367
Damien Miller1383bd82000-04-06 12:32:37 +10001368 if (quit_pending)
1369 break;
1370
1371 if (!compat20) {
1372 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001373 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001374 /*
1375 * Process output to stdout and stderr. Output to
1376 * the connection is processed elsewhere (above).
1377 */
Damien Miller5e953212001-01-30 09:14:00 +11001378 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001379 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Damien Miller5428f641999-11-25 11:54:57 +11001381 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001382 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001383 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384 }
Damien Miller5e953212001-01-30 09:14:00 +11001385 if (readset)
1386 xfree(readset);
1387 if (writeset)
1388 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Damien Miller95def091999-11-25 00:26:21 +11001390 /* Terminate the session. */
1391
1392 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001393 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001394
Ben Lindstrom601e4362001-06-21 03:19:23 +00001395 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001396
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001397 if (have_pty)
1398 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001399
1400 /* restore blocking io */
1401 if (!isatty(fileno(stdin)))
1402 unset_nonblock(fileno(stdin));
1403 if (!isatty(fileno(stdout)))
1404 unset_nonblock(fileno(stdout));
1405 if (!isatty(fileno(stderr)))
1406 unset_nonblock(fileno(stderr));
1407
Damien Millerd6965512003-12-17 16:31:53 +11001408 /*
1409 * If there was no shell or command requested, there will be no remote
1410 * exit status to be returned. In that case, clear error code if the
1411 * connection was deliberately terminated at this end.
1412 */
1413 if (no_shell_flag && received_signal == SIGTERM) {
1414 received_signal = 0;
1415 exit_status = 0;
1416 }
1417
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001418 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001419 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001420
1421 /*
1422 * In interactive mode (with pseudo tty) display a message indicating
1423 * that the connection has been closed.
1424 */
1425 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1426 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1427 buffer_append(&stderr_buffer, buf, strlen(buf));
1428 }
1429
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001431 while (buffer_len(&stdout_buffer) > 0) {
1432 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001433 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001434 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001435 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001436 break;
1437 }
Damien Miller95def091999-11-25 00:26:21 +11001438 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001439 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001440 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Damien Miller95def091999-11-25 00:26:21 +11001442 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001443 while (buffer_len(&stderr_buffer) > 0) {
1444 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001445 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001446 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001447 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001448 break;
1449 }
Damien Miller95def091999-11-25 00:26:21 +11001450 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001451 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001452 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Damien Miller95def091999-11-25 00:26:21 +11001454 /* Clear and free any buffers. */
1455 memset(buf, 0, sizeof(buf));
1456 buffer_free(&stdin_buffer);
1457 buffer_free(&stdout_buffer);
1458 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459
Damien Miller95def091999-11-25 00:26:21 +11001460 /* Report bytes transferred, and transfer rates. */
1461 total_time = get_current_time() - start_time;
1462 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001463 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001464 if (total_time > 0)
1465 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001466 stdin_bytes / total_time, stdout_bytes / total_time,
1467 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001468
1469 /* Return the exit status of the program. */
1470 debug("Exit status %d", exit_status);
1471 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472}
Damien Millerb38eff82000-04-01 11:09:21 +10001473
1474/*********/
1475
Ben Lindstrombba81212001-06-25 05:01:22 +00001476static void
Damien Miller630d6f42002-01-22 23:17:30 +11001477client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001478{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001479 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001480 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001481 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001482 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001483 memset(data, 0, data_len);
1484 xfree(data);
1485}
Ben Lindstrombba81212001-06-25 05:01:22 +00001486static void
Damien Miller630d6f42002-01-22 23:17:30 +11001487client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001488{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001489 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001490 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001491 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001492 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001493 memset(data, 0, data_len);
1494 xfree(data);
1495}
Ben Lindstrombba81212001-06-25 05:01:22 +00001496static void
Damien Miller630d6f42002-01-22 23:17:30 +11001497client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001498{
Damien Millerb38eff82000-04-01 11:09:21 +10001499 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001500 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001501 /* Acknowledge the exit. */
1502 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1503 packet_send();
1504 /*
1505 * Must wait for packet to be sent since we are
1506 * exiting the loop.
1507 */
1508 packet_write_wait();
1509 /* Flag that we want to exit. */
1510 quit_pending = 1;
1511}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001512static void
1513client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1514{
1515 Channel *c = NULL;
1516 int remote_id, sock;
1517
1518 /* Read the remote channel number from the message. */
1519 remote_id = packet_get_int();
1520 packet_check_eom();
1521
1522 /*
1523 * Get a connection to the local authentication agent (this may again
1524 * get forwarded).
1525 */
1526 sock = ssh_get_authentication_socket();
1527
1528 /*
1529 * If we could not connect the agent, send an error message back to
1530 * the server. This should never happen unless the agent dies,
1531 * because authentication forwarding is only enabled if we have an
1532 * agent.
1533 */
1534 if (sock >= 0) {
1535 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1536 -1, 0, 0, 0, "authentication agent connection", 1);
1537 c->remote_id = remote_id;
1538 c->force_drain = 1;
1539 }
1540 if (c == NULL) {
1541 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1542 packet_put_int(remote_id);
1543 } else {
1544 /* Send a confirmation to the remote host. */
1545 debug("Forwarding authentication connection.");
1546 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1547 packet_put_int(remote_id);
1548 packet_put_int(c->self);
1549 }
1550 packet_send();
1551}
Damien Millerb38eff82000-04-01 11:09:21 +10001552
Ben Lindstrombba81212001-06-25 05:01:22 +00001553static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001554client_request_forwarded_tcpip(const char *request_type, int rchan)
1555{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001556 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001557 char *listen_address, *originator_address;
1558 int listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001559
1560 /* Get rest of the packet */
1561 listen_address = packet_get_string(NULL);
1562 listen_port = packet_get_int();
1563 originator_address = packet_get_string(NULL);
1564 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001565 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001566
Damien Millerbd740252008-05-19 15:37:09 +10001567 debug("client_request_forwarded_tcpip: listen %s port %d, "
1568 "originator %s port %d", listen_address, listen_port,
1569 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001570
Damien Millerbd740252008-05-19 15:37:09 +10001571 c = channel_connect_by_listen_address(listen_port,
1572 "forwarded-tcpip", originator_address);
1573
Damien Miller0bc1bd82000-11-13 22:57:25 +11001574 xfree(originator_address);
1575 xfree(listen_address);
1576 return c;
1577}
1578
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001579static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001580client_request_x11(const char *request_type, int rchan)
1581{
1582 Channel *c = NULL;
1583 char *originator;
1584 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001585 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001586
1587 if (!options.forward_x11) {
1588 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001589 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001590 return NULL;
1591 }
1592 originator = packet_get_string(NULL);
1593 if (datafellows & SSH_BUG_X11FWD) {
1594 debug2("buggy server: x11 request w/o originator_port");
1595 originator_port = 0;
1596 } else {
1597 originator_port = packet_get_int();
1598 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001599 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001600 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001601 debug("client_request_x11: request from %s %d", originator,
1602 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001603 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001604 sock = x11_connect_display();
1605 if (sock < 0)
1606 return NULL;
1607 c = channel_new("x11",
1608 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001609 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001610 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001611 return c;
1612}
1613
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001614static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001615client_request_agent(const char *request_type, int rchan)
1616{
1617 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001618 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001619
1620 if (!options.forward_agent) {
1621 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001622 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001623 return NULL;
1624 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001625 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001626 if (sock < 0)
1627 return NULL;
1628 c = channel_new("authentication agent connection",
1629 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001630 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001631 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001632 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001633 return c;
1634}
1635
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001636int
1637client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1638{
1639 Channel *c;
1640 int fd;
1641
1642 if (tun_mode == SSH_TUNMODE_NO)
1643 return 0;
1644
1645 if (!compat20) {
1646 error("Tunnel forwarding is not support for protocol 1");
1647 return -1;
1648 }
1649
1650 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1651
1652 /* Open local tunnel device */
1653 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1654 error("Tunnel device open failed.");
1655 return -1;
1656 }
1657
1658 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1659 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1660 c->datagram = 1;
1661
1662#if defined(SSH_TUN_FILTER)
1663 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1664 channel_register_filter(c->self, sys_tun_infilter,
1665 sys_tun_outfilter);
1666#endif
1667
1668 packet_start(SSH2_MSG_CHANNEL_OPEN);
1669 packet_put_cstring("tun@openssh.com");
1670 packet_put_int(c->self);
1671 packet_put_int(c->local_window_max);
1672 packet_put_int(c->local_maxpacket);
1673 packet_put_int(tun_mode);
1674 packet_put_int(remote_tun);
1675 packet_send();
1676
1677 return 0;
1678}
1679
Damien Millerbd483e72000-04-30 10:00:53 +10001680/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001681static void
Damien Miller630d6f42002-01-22 23:17:30 +11001682client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001683{
1684 Channel *c = NULL;
1685 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001686 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001687 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001688
1689 ctype = packet_get_string(&len);
1690 rchan = packet_get_int();
1691 rwindow = packet_get_int();
1692 rmaxpack = packet_get_int();
1693
Damien Millere247cc42000-05-07 12:03:14 +10001694 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001695 ctype, rchan, rwindow, rmaxpack);
1696
Damien Miller0bc1bd82000-11-13 22:57:25 +11001697 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1698 c = client_request_forwarded_tcpip(ctype, rchan);
1699 } else if (strcmp(ctype, "x11") == 0) {
1700 c = client_request_x11(ctype, rchan);
1701 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1702 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001703 }
1704/* XXX duplicate : */
1705 if (c != NULL) {
1706 debug("confirm %s", ctype);
1707 c->remote_id = rchan;
1708 c->remote_window = rwindow;
1709 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001710 if (c->type != SSH_CHANNEL_CONNECTING) {
1711 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1712 packet_put_int(c->remote_id);
1713 packet_put_int(c->self);
1714 packet_put_int(c->local_window);
1715 packet_put_int(c->local_maxpacket);
1716 packet_send();
1717 }
Damien Millerbd483e72000-04-30 10:00:53 +10001718 } else {
1719 debug("failure %s", ctype);
1720 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1721 packet_put_int(rchan);
1722 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001723 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001724 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001725 packet_put_cstring("");
1726 }
Damien Millerbd483e72000-04-30 10:00:53 +10001727 packet_send();
1728 }
1729 xfree(ctype);
1730}
Ben Lindstrombba81212001-06-25 05:01:22 +00001731static void
Damien Miller630d6f42002-01-22 23:17:30 +11001732client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001733{
1734 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001735 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001736 char *rtype;
1737
1738 id = packet_get_int();
1739 rtype = packet_get_string(NULL);
1740 reply = packet_get_char();
1741
1742 debug("client_input_channel_req: channel %d rtype %s reply %d",
1743 id, rtype, reply);
1744
Damien Miller3bbd8782004-06-18 22:23:22 +10001745 if (id == -1) {
1746 error("client_input_channel_req: request for channel -1");
1747 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001748 error("client_input_channel_req: channel %d: unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001749 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1750 packet_check_eom();
1751 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001752 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001753 exitval = packet_get_int();
1754 if (id == session_ident) {
1755 success = 1;
1756 exit_status = exitval;
1757 } else if (c->ctl_fd == -1) {
1758 error("client_input_channel_req: unexpected channel %d",
1759 session_ident);
1760 } else {
1761 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1762 success = 1;
1763 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001764 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001765 }
1766 if (reply) {
1767 packet_start(success ?
1768 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001769 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001770 packet_send();
1771 }
1772 xfree(rtype);
1773}
Damien Millerc3fa4072002-01-22 23:21:58 +11001774static void
1775client_input_global_request(int type, u_int32_t seq, void *ctxt)
1776{
1777 char *rtype;
1778 int want_reply;
1779 int success = 0;
1780
1781 rtype = packet_get_string(NULL);
1782 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001783 debug("client_input_global_request: rtype %s want_reply %d",
1784 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001785 if (want_reply) {
1786 packet_start(success ?
1787 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1788 packet_send();
1789 packet_write_wait();
1790 }
1791 xfree(rtype);
1792}
Damien Millerbd483e72000-04-30 10:00:53 +10001793
Damien Miller0e220db2004-06-15 10:34:08 +10001794void
Darren Tuckerfc959702004-07-17 16:12:08 +10001795client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001796 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001797{
1798 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001799 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001800
1801 debug2("%s: id %d", __func__, id);
1802
Darren Tucker5d78de62004-11-05 20:35:44 +11001803 if ((c = channel_lookup(id)) == NULL)
1804 fatal("client_session2_setup: channel %d: unknown channel", id);
1805
Damien Miller0e220db2004-06-15 10:34:08 +10001806 if (want_tty) {
1807 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001808
1809 /* Store window size in the packet. */
1810 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1811 memset(&ws, 0, sizeof(ws));
1812
Damien Miller5771ed72008-05-19 15:35:33 +10001813 channel_request_start(id, "pty-req", 1);
1814 client_expect_confirm(id, "PTY allocation", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10001815 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001816 packet_put_int((u_int)ws.ws_col);
1817 packet_put_int((u_int)ws.ws_row);
1818 packet_put_int((u_int)ws.ws_xpixel);
1819 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001820 if (tiop == NULL)
1821 tiop = get_saved_tio();
1822 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001823 packet_send();
1824 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001825 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001826 }
1827
1828 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001829 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001830 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001831 char *name, *val;
1832
1833 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001834 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001835 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001836 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001837 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001838 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001839 continue;
1840 }
1841 *val++ = '\0';
1842
1843 matched = 0;
1844 for (j = 0; j < options.num_send_env; j++) {
1845 if (match_pattern(name, options.send_env[j])) {
1846 matched = 1;
1847 break;
1848 }
1849 }
1850 if (!matched) {
1851 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001852 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001853 continue;
1854 }
1855
1856 debug("Sending env %s = %s", name, val);
1857 channel_request_start(id, "env", 0);
1858 packet_put_cstring(name);
1859 packet_put_cstring(val);
1860 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001861 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001862 }
1863 }
1864
1865 len = buffer_len(cmd);
1866 if (len > 0) {
1867 if (len > 900)
1868 len = 900;
1869 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10001870 debug("Sending subsystem: %.*s",
1871 len, (u_char*)buffer_ptr(cmd));
1872 channel_request_start(id, "subsystem", 1);
1873 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001874 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001875 debug("Sending command: %.*s",
1876 len, (u_char*)buffer_ptr(cmd));
1877 channel_request_start(id, "exec", 1);
1878 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001879 }
1880 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1881 packet_send();
1882 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001883 channel_request_start(id, "shell", 1);
1884 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001885 packet_send();
1886 }
1887}
1888
Ben Lindstrombba81212001-06-25 05:01:22 +00001889static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001890client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001891{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001892 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001893
Damien Miller1383bd82000-04-06 12:32:37 +10001894 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1895 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1896 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1897 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001898 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001899 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1900 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001901 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001902 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10001903 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
1904 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11001905 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001906
1907 /* rekeying */
1908 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001909
1910 /* global request reply messages */
1911 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1912 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001913}
Ben Lindstrombba81212001-06-25 05:01:22 +00001914static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001915client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001916{
1917 dispatch_init(NULL);
1918 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1919 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1920 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001921 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1922 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1923 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001924 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1925 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1926 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001927
1928 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10001929 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11001930 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1931 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001932}
Ben Lindstrombba81212001-06-25 05:01:22 +00001933static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001934client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001935{
1936 client_init_dispatch_13();
1937 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1938 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1939}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001940static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001941client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001942{
Damien Miller1383bd82000-04-06 12:32:37 +10001943 if (compat20)
1944 client_init_dispatch_20();
1945 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001946 client_init_dispatch_13();
1947 else
1948 client_init_dispatch_15();
1949}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001950
1951/* client specific fatal cleanup */
1952void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001953cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001954{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001955 leave_raw_mode();
1956 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10001957 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001958 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10001959 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001960}