blob: 46f52e1a25a4e3ed23ecec1aaa5be3daa6b81e79 [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: clientloop.c,v 1.173 2006/07/26 13:57:17 stevesk 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 Millere7a1e5c2006-08-05 11:34:19 +100081#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100082#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110083#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100084#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "ssh1.h"
88#include "ssh2.h"
89#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090#include "packet.h"
91#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100092#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000093#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100094#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100095#include "buffer.h"
96#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110097#include "key.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +000098#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000099#include "log.h"
100#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000101#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100102#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000103#include "authfd.h"
104#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000105#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000106#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000107#include "monitor_fdpass.h"
108#include "match.h"
109#include "msg.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100110
111/* import options */
112extern Options options;
113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114/* Flag indicating that stdin should be redirected from /dev/null. */
115extern int stdin_null_flag;
116
Damien Millerd6965512003-12-17 16:31:53 +1100117/* Flag indicating that no shell has been requested */
118extern int no_shell_flag;
119
Damien Miller0e220db2004-06-15 10:34:08 +1000120/* Control socket */
121extern int control_fd;
122
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * Name of the host we are connecting to. This is the name given on the
125 * command line, or the HostName specified for the user-supplied name in a
126 * configuration file.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128extern char *host;
129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Flag to indicate that we have received a window change signal which has
132 * not yet been processed. This will cause a message indicating the new
133 * window size to be sent to the server a little later. This is volatile
134 * because this is updated in a signal handler.
135 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000136static volatile sig_atomic_t received_window_change_signal = 0;
137static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller788f2122005-11-05 15:14:59 +1100139/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140static int in_non_blocking_mode = 0;
141
142/* Common data for the client loop code. */
Damien Miller97c91f62006-04-23 12:08:37 +1000143static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Millerad833b32000-08-23 10:46:23 +1000144static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100145static int escape_pending; /* Last character was the escape character */
146static int last_was_cr; /* Last character was a newline. */
147static int exit_status; /* Used to store the exit status of the command. */
148static int stdin_eof; /* EOF has been encountered on standard error. */
149static Buffer stdin_buffer; /* Buffer for stdin data. */
150static Buffer stdout_buffer; /* Buffer for stdout data. */
151static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000152static u_long stdin_bytes, stdout_bytes, stderr_bytes;
153static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100154static int connection_in; /* Connection to server (input). */
155static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000156static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000157static int session_closed = 0; /* In SSH2: login session closed. */
Damien Miller509b0102003-12-17 16:33:10 +1100158static int server_alive_timeouts = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000161int session_ident = -1;
162
Damien Miller0e220db2004-06-15 10:34:08 +1000163struct confirm_ctx {
164 int want_tty;
165 int want_subsys;
Damien Miller13390022005-07-06 09:44:19 +1000166 int want_x_fwd;
167 int want_agent_fwd;
Damien Miller0e220db2004-06-15 10:34:08 +1000168 Buffer cmd;
169 char *term;
170 struct termios tio;
Damien Miller3756dce2004-06-18 01:17:29 +1000171 char **env;
Damien Miller0e220db2004-06-15 10:34:08 +1000172};
173
Ben Lindstromf28f6342001-04-04 02:03:04 +0000174/*XXX*/
175extern Kex *xxx_kex;
176
Damien Miller0e220db2004-06-15 10:34:08 +1000177void ssh_process_session2_setup(int, int, int, Buffer *);
178
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179/* Restores stdin to blocking mode. */
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000182leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183{
Damien Miller95def091999-11-25 00:26:21 +1100184 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000185 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100186 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100187 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188}
189
Damien Miller95def091999-11-25 00:26:21 +1100190/* Puts stdin terminal in non-blocking mode. */
191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000193enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Damien Miller95def091999-11-25 00:26:21 +1100195 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000196 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Damien Miller5428f641999-11-25 11:54:57 +1100199/*
200 * Signal handler for the window change signal (SIGWINCH). This just sets a
201 * flag indicating that the window has changed.
202 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100203/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static void
Damien Miller95def091999-11-25 00:26:21 +1100205window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller95def091999-11-25 00:26:21 +1100207 received_window_change_signal = 1;
208 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
212 * Signal handler for signals that cause the program to terminate. These
213 * signals must be trapped to restore terminal modes.
214 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100215/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static void
Damien Miller95def091999-11-25 00:26:21 +1100217signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000219 received_signal = sig;
220 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221}
222
Damien Miller5428f641999-11-25 11:54:57 +1100223/*
224 * Returns current time in seconds from Jan 1, 1970 with the maximum
225 * available resolution.
226 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000229get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230{
Damien Miller95def091999-11-25 00:26:21 +1100231 struct timeval tv;
232 gettimeofday(&tv, NULL);
233 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234}
235
Damien Miller17e7ed02005-06-17 12:54:33 +1000236#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
237void
238client_x11_get_proto(const char *display, const char *xauth_path,
239 u_int trusted, char **_proto, char **_data)
240{
241 char cmd[1024];
242 char line[512];
243 char xdisplay[512];
244 static char proto[512], data[512];
245 FILE *f;
246 int got_data = 0, generated = 0, do_unlink = 0, i;
247 char *xauthdir, *xauthfile;
248 struct stat st;
249
250 xauthdir = xauthfile = NULL;
251 *_proto = proto;
252 *_data = data;
253 proto[0] = data[0] = '\0';
254
255 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
256 debug("No xauth program.");
257 } else {
258 if (display == NULL) {
259 debug("x11_get_proto: DISPLAY not set");
260 return;
261 }
262 /*
263 * Handle FamilyLocal case where $DISPLAY does
264 * not match an authorization entry. For this we
265 * just try "xauth list unix:displaynum.screennum".
266 * XXX: "localhost" match to determine FamilyLocal
267 * is not perfect.
268 */
269 if (strncmp(display, "localhost:", 10) == 0) {
270 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
271 display + 10);
272 display = xdisplay;
273 }
274 if (trusted == 0) {
275 xauthdir = xmalloc(MAXPATHLEN);
276 xauthfile = xmalloc(MAXPATHLEN);
277 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
278 if (mkdtemp(xauthdir) != NULL) {
279 do_unlink = 1;
280 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
281 xauthdir);
282 snprintf(cmd, sizeof(cmd),
283 "%s -f %s generate %s " SSH_X11_PROTO
284 " untrusted timeout 1200 2>" _PATH_DEVNULL,
285 xauth_path, xauthfile, display);
286 debug2("x11_get_proto: %s", cmd);
287 if (system(cmd) == 0)
288 generated = 1;
289 }
290 }
291 snprintf(cmd, sizeof(cmd),
Darren Tuckerd89dbf22005-10-03 18:05:26 +1000292 "%s %s%s list %s 2>" _PATH_DEVNULL,
Damien Miller17e7ed02005-06-17 12:54:33 +1000293 xauth_path,
294 generated ? "-f " : "" ,
295 generated ? xauthfile : "",
296 display);
297 debug2("x11_get_proto: %s", cmd);
298 f = popen(cmd, "r");
299 if (f && fgets(line, sizeof(line), f) &&
300 sscanf(line, "%*s %511s %511s", proto, data) == 2)
301 got_data = 1;
302 if (f)
303 pclose(f);
304 }
305
306 if (do_unlink) {
307 unlink(xauthfile);
308 rmdir(xauthdir);
309 }
310 if (xauthdir)
311 xfree(xauthdir);
312 if (xauthfile)
313 xfree(xauthfile);
314
315 /*
316 * If we didn't get authentication data, just make up some
317 * data. The forwarding code will check the validity of the
318 * response anyway, and substitute this data. The X11
319 * server, however, will ignore this fake data and use
320 * whatever authentication mechanisms it was using otherwise
321 * for the local connection.
322 */
323 if (!got_data) {
324 u_int32_t rnd = 0;
325
326 logit("Warning: No xauth data; "
327 "using fake authentication data for X11 forwarding.");
328 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
329 for (i = 0; i < 16; i++) {
330 if (i % 4 == 0)
331 rnd = arc4random();
332 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
333 rnd & 0xff);
334 rnd >>= 8;
335 }
336 }
337}
338
Damien Miller5428f641999-11-25 11:54:57 +1100339/*
340 * This is called when the interactive is entered. This checks if there is
341 * an EOF coming on stdin. We must check this explicitly, as select() does
342 * not appear to wake up when redirecting from /dev/null.
343 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Ben Lindstrombba81212001-06-25 05:01:22 +0000345static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000346client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347{
Damien Miller95def091999-11-25 00:26:21 +1100348 int len;
349 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller5428f641999-11-25 11:54:57 +1100351 /*
352 * If standard input is to be "redirected from /dev/null", we simply
353 * mark that we have seen an EOF and send an EOF message to the
354 * server. Otherwise, we try to read a single character; it appears
355 * that for some files, such /dev/null, select() never wakes up for
356 * read for this descriptor, which means that we never get EOF. This
357 * way we will get the EOF if stdin comes from /dev/null or similar.
358 */
Damien Miller95def091999-11-25 00:26:21 +1100359 if (stdin_null_flag) {
360 /* Fake EOF on stdin. */
361 debug("Sending eof.");
362 stdin_eof = 1;
363 packet_start(SSH_CMSG_EOF);
364 packet_send();
365 } else {
Damien Miller95def091999-11-25 00:26:21 +1100366 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Damien Miller95def091999-11-25 00:26:21 +1100368 /* Check for immediate EOF on stdin. */
369 len = read(fileno(stdin), buf, 1);
370 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100371 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100372 debug("Sending eof.");
373 stdin_eof = 1;
374 packet_start(SSH_CMSG_EOF);
375 packet_send();
376 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100377 /*
378 * Got data. We must store the data in the buffer,
379 * and also process it as an escape character if
380 * appropriate.
381 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100383 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000384 else
Damien Miller95def091999-11-25 00:26:21 +1100385 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100386 }
Damien Miller95def091999-11-25 00:26:21 +1100387 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389}
390
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller5428f641999-11-25 11:54:57 +1100392/*
393 * Make packets from buffered stdin data, and buffer them for sending to the
394 * connection.
395 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Ben Lindstrombba81212001-06-25 05:01:22 +0000397static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000398client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000400 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Damien Miller95def091999-11-25 00:26:21 +1100402 /* Send buffered stdin data to the server. */
403 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100404 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100405 len = buffer_len(&stdin_buffer);
406 /* Keep the packets at reasonable size. */
407 if (len > packet_get_maxsize())
408 len = packet_get_maxsize();
409 packet_start(SSH_CMSG_STDIN_DATA);
410 packet_put_string(buffer_ptr(&stdin_buffer), len);
411 packet_send();
412 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000413 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100414 /* If we have a pending EOF, send it now. */
415 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
416 packet_start(SSH_CMSG_EOF);
417 packet_send();
418 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420}
421
Damien Miller5428f641999-11-25 11:54:57 +1100422/*
423 * Checks if the client window has changed, and sends a packet about it to
424 * the server if so. The actual change is detected elsewhere (by a software
425 * interrupt on Unix); this just checks the flag and sends a message if
426 * appropriate.
427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Ben Lindstrombba81212001-06-25 05:01:22 +0000429static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000430client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller1383bd82000-04-06 12:32:37 +1000432 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller1383bd82000-04-06 12:32:37 +1000434 if (! received_window_change_signal)
435 return;
436 /** XXX race */
437 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Millerd3444942000-09-30 14:20:03 +1100439 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000440
441 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000442 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000443 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000444 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
445 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000446 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100447 packet_put_int((u_int)ws.ws_row);
448 packet_put_int((u_int)ws.ws_col);
449 packet_put_int((u_int)ws.ws_xpixel);
450 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000451 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453}
454
Damien Miller509b0102003-12-17 16:33:10 +1100455static void
456client_global_request_reply(int type, u_int32_t seq, void *ctxt)
457{
458 server_alive_timeouts = 0;
459 client_global_request_reply_fwd(type, seq, ctxt);
460}
461
462static void
463server_alive_check(void)
464{
465 if (++server_alive_timeouts > options.server_alive_count_max)
466 packet_disconnect("Timeout, server not responding.");
467 packet_start(SSH2_MSG_GLOBAL_REQUEST);
468 packet_put_cstring("keepalive@openssh.com");
469 packet_put_char(1); /* boolean: want reply */
470 packet_send();
471}
472
Damien Miller5428f641999-11-25 11:54:57 +1100473/*
474 * Waits until the client can do something (some data becomes available on
475 * one of the file descriptors).
476 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000477static void
Damien Miller5e953212001-01-30 09:14:00 +1100478client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000479 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480{
Damien Miller509b0102003-12-17 16:33:10 +1100481 struct timeval tv, *tvp;
482 int ret;
483
Damien Miller5e953212001-01-30 09:14:00 +1100484 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000485 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller1383bd82000-04-06 12:32:37 +1000487 if (!compat20) {
488 /* Read from the connection, unless our buffers are full. */
489 if (buffer_len(&stdout_buffer) < buffer_high &&
490 buffer_len(&stderr_buffer) < buffer_high &&
491 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100492 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000493 /*
494 * Read from stdin, unless we have seen EOF or have very much
495 * buffered data to send to the server.
496 */
497 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100498 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000499
500 /* Select stdout/stderr if have data in buffer. */
501 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100502 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000503 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100504 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000505 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000506 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000507 if (session_closed && !channel_still_open() &&
508 !packet_have_data_to_write()) {
509 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100510 memset(*readsetp, 0, *nallocp);
511 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000512 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000513 } else {
514 FD_SET(connection_in, *readsetp);
515 }
Damien Miller1383bd82000-04-06 12:32:37 +1000516 }
517
Damien Miller95def091999-11-25 00:26:21 +1100518 /* Select server connection if have data to write to the server. */
519 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100520 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100521
Damien Miller0e220db2004-06-15 10:34:08 +1000522 if (control_fd != -1)
523 FD_SET(control_fd, *readsetp);
524
Damien Miller5428f641999-11-25 11:54:57 +1100525 /*
526 * Wait for something to happen. This will suspend the process until
527 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100528 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100529 */
Damien Miller95def091999-11-25 00:26:21 +1100530
Damien Miller509b0102003-12-17 16:33:10 +1100531 if (options.server_alive_interval == 0 || !compat20)
532 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000533 else {
Damien Miller509b0102003-12-17 16:33:10 +1100534 tv.tv_sec = options.server_alive_interval;
535 tv.tv_usec = 0;
536 tvp = &tv;
537 }
538 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
539 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100540 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000541
542 /*
543 * We have to clear the select masks, because we return.
544 * We have to return, because the mainloop checks for the flags
545 * set by the signal handlers.
546 */
Damien Miller79faeff2001-11-12 11:06:32 +1100547 memset(*readsetp, 0, *nallocp);
548 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000549
Damien Miller95def091999-11-25 00:26:21 +1100550 if (errno == EINTR)
551 return;
552 /* Note: we might still have data in the buffers. */
553 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
554 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100555 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100556 } else if (ret == 0)
557 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558}
559
Ben Lindstrombba81212001-06-25 05:01:22 +0000560static void
Damien Millerad833b32000-08-23 10:46:23 +1000561client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562{
Damien Miller95def091999-11-25 00:26:21 +1100563 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000564 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000565 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000566 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000567 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Damien Miller95def091999-11-25 00:26:21 +1100569 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller5428f641999-11-25 11:54:57 +1100571 /*
572 * Free (and clear) the buffer to reduce the amount of data that gets
573 * written to swap.
574 */
Damien Millerad833b32000-08-23 10:46:23 +1000575 buffer_free(bin);
576 buffer_free(bout);
577 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Send the suspend signal to the program itself. */
580 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Darren Tucker5d78de62004-11-05 20:35:44 +1100582 /* Reset window sizes in case they have changed */
583 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Damien Miller95def091999-11-25 00:26:21 +1100585 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000586 buffer_init(bin);
587 buffer_init(bout);
588 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller95def091999-11-25 00:26:21 +1100590 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591}
592
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100594client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595{
Damien Miller1383bd82000-04-06 12:32:37 +1000596 int len;
597 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598
Damien Miller5428f641999-11-25 11:54:57 +1100599 /*
600 * Read input from the server, and add any such data to the buffer of
601 * the packet subsystem.
602 */
Damien Miller95def091999-11-25 00:26:21 +1100603 if (FD_ISSET(connection_in, readset)) {
604 /* Read as much as possible. */
605 len = read(connection_in, buf, sizeof(buf));
606 if (len == 0) {
607 /* Received EOF. The remote host has closed the connection. */
608 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
609 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611 quit_pending = 1;
612 return;
Damien Miller95def091999-11-25 00:26:21 +1100613 }
Damien Miller5428f641999-11-25 11:54:57 +1100614 /*
615 * There is a kernel bug on Solaris that causes select to
616 * sometimes wake up even though there is no data available.
617 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000618 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100619 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller95def091999-11-25 00:26:21 +1100621 if (len < 0) {
622 /* An error has encountered. Perhaps there is a network problem. */
623 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
624 host, strerror(errno));
625 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100626 quit_pending = 1;
627 return;
628 }
629 packet_process_incoming(buf, len);
630 }
Damien Miller1383bd82000-04-06 12:32:37 +1000631}
632
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000633static void
Damien Miller0e220db2004-06-15 10:34:08 +1000634client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
635{
636 int id;
637 Channel *c;
Darren Tuckerfc959702004-07-17 16:12:08 +1000638
Damien Miller0e220db2004-06-15 10:34:08 +1000639 id = packet_get_int();
640 packet_check_eom();
641
642 if ((c = channel_lookup(id)) == NULL) {
643 error("%s: no channel for id %d", __func__, id);
644 return;
645 }
646
647 if (type == SSH2_MSG_CHANNEL_SUCCESS)
648 debug2("Request suceeded on channel %d", id);
649 else if (type == SSH2_MSG_CHANNEL_FAILURE) {
650 error("Request failed on channel %d", id);
651 channel_free(c);
652 }
653}
654
655static void
656client_extra_session2_setup(int id, void *arg)
657{
658 struct confirm_ctx *cctx = arg;
Damien Miller13390022005-07-06 09:44:19 +1000659 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +1000660 Channel *c;
Damien Miller3756dce2004-06-18 01:17:29 +1000661 int i;
Darren Tuckerfc959702004-07-17 16:12:08 +1000662
Damien Miller0e220db2004-06-15 10:34:08 +1000663 if (cctx == NULL)
664 fatal("%s: cctx == NULL", __func__);
665 if ((c = channel_lookup(id)) == NULL)
666 fatal("%s: no channel for id %d", __func__, id);
667
Damien Miller46d38de2005-07-17 17:02:09 +1000668 display = getenv("DISPLAY");
Damien Miller13390022005-07-06 09:44:19 +1000669 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
670 char *proto, *data;
671 /* Get reasonable local authentication information. */
672 client_x11_get_proto(display, options.xauth_location,
673 options.forward_x11_trusted, &proto, &data);
674 /* Request forwarding with authentication spoofing. */
675 debug("Requesting X11 forwarding with authentication spoofing.");
676 x11_request_forwarding_with_spoofing(id, display, proto, data);
677 /* XXX wait for reply */
678 }
679
680 if (cctx->want_agent_fwd && options.forward_agent) {
681 debug("Requesting authentication agent forwarding.");
682 channel_request_start(id, "auth-agent-req@openssh.com", 0);
683 packet_send();
684 }
685
Darren Tuckerfc959702004-07-17 16:12:08 +1000686 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
Damien Miller3756dce2004-06-18 01:17:29 +1000687 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
Damien Miller0e220db2004-06-15 10:34:08 +1000688 client_subsystem_reply);
Damien Miller23f07702004-06-18 01:19:03 +1000689
Damien Miller0e220db2004-06-15 10:34:08 +1000690 c->confirm_ctx = NULL;
691 buffer_free(&cctx->cmd);
Damien Miller3756dce2004-06-18 01:17:29 +1000692 xfree(cctx->term);
693 if (cctx->env != NULL) {
694 for (i = 0; cctx->env[i] != NULL; i++)
695 xfree(cctx->env[i]);
696 xfree(cctx->env);
Darren Tuckerfc959702004-07-17 16:12:08 +1000697 }
Damien Miller3756dce2004-06-18 01:17:29 +1000698 xfree(cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000699}
700
701static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100702client_process_control(fd_set *readset)
Damien Miller0e220db2004-06-15 10:34:08 +1000703{
704 Buffer m;
705 Channel *c;
Damien Millereccb9de2005-06-17 12:59:34 +1000706 int client_fd, new_fd[3], ver, allowed;
Damien Miller0e220db2004-06-15 10:34:08 +1000707 socklen_t addrlen;
708 struct sockaddr_storage addr;
709 struct confirm_ctx *cctx;
710 char *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +1000711 u_int i, len, env_len, command, flags;
Damien Miller0e220db2004-06-15 10:34:08 +1000712 uid_t euid;
713 gid_t egid;
714
715 /*
716 * Accept connection on control socket
717 */
718 if (control_fd == -1 || !FD_ISSET(control_fd, readset))
719 return;
720
721 memset(&addr, 0, sizeof(addr));
722 addrlen = sizeof(addr);
723 if ((client_fd = accept(control_fd,
724 (struct sockaddr*)&addr, &addrlen)) == -1) {
725 error("%s accept: %s", __func__, strerror(errno));
726 return;
727 }
728
729 if (getpeereid(client_fd, &euid, &egid) < 0) {
730 error("%s getpeereid failed: %s", __func__, strerror(errno));
731 close(client_fd);
732 return;
733 }
734 if ((euid != 0) && (getuid() != euid)) {
735 error("control mode uid mismatch: peer euid %u != uid %u",
736 (u_int) euid, (u_int) getuid());
737 close(client_fd);
738 return;
739 }
Damien Miller23f07702004-06-18 01:19:03 +1000740
Damien Miller0e220db2004-06-15 10:34:08 +1000741 unset_nonblock(client_fd);
742
Darren Tucker7ebfc102004-11-07 20:06:19 +1100743 /* Read command */
Damien Miller0e220db2004-06-15 10:34:08 +1000744 buffer_init(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100745 if (ssh_msg_recv(client_fd, &m) == -1) {
746 error("%s: client msg_recv failed", __func__);
747 close(client_fd);
748 buffer_free(&m);
749 return;
750 }
Damien Miller13390022005-07-06 09:44:19 +1000751 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100752 error("%s: wrong client version %d", __func__, ver);
753 buffer_free(&m);
754 close(client_fd);
755 return;
756 }
Damien Miller0e220db2004-06-15 10:34:08 +1000757
Darren Tucker7ebfc102004-11-07 20:06:19 +1100758 allowed = 1;
759 command = buffer_get_int(&m);
760 flags = buffer_get_int(&m);
761
762 buffer_clear(&m);
763
764 switch (command) {
765 case SSHMUX_COMMAND_OPEN:
Damien Millerd14b1e72005-06-16 13:19:41 +1000766 if (options.control_master == SSHCTL_MASTER_ASK ||
767 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100768 allowed = ask_permission("Allow shared connection "
769 "to %s? ", host);
770 /* continue below */
771 break;
772 case SSHMUX_COMMAND_TERMINATE:
Damien Millerd14b1e72005-06-16 13:19:41 +1000773 if (options.control_master == SSHCTL_MASTER_ASK ||
774 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100775 allowed = ask_permission("Terminate shared connection "
776 "to %s? ", host);
777 if (allowed)
778 quit_pending = 1;
Darren Tucker47eede72005-03-14 23:08:12 +1100779 /* FALLTHROUGH */
Darren Tucker7ebfc102004-11-07 20:06:19 +1100780 case SSHMUX_COMMAND_ALIVE_CHECK:
781 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
782 buffer_clear(&m);
783 buffer_put_int(&m, allowed);
784 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000785 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100786 error("%s: client msg_send failed", __func__);
787 close(client_fd);
788 buffer_free(&m);
789 return;
790 }
791 buffer_free(&m);
792 close(client_fd);
793 return;
794 default:
795 error("Unsupported command %d", command);
796 buffer_free(&m);
797 close(client_fd);
798 return;
799 }
800
801 /* Reply for SSHMUX_COMMAND_OPEN */
802 buffer_clear(&m);
Damien Miller23f07702004-06-18 01:19:03 +1000803 buffer_put_int(&m, allowed);
Damien Miller0e220db2004-06-15 10:34:08 +1000804 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000805 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000806 error("%s: client msg_send failed", __func__);
807 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000808 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000809 return;
810 }
Damien Miller0e220db2004-06-15 10:34:08 +1000811
Damien Miller23f07702004-06-18 01:19:03 +1000812 if (!allowed) {
813 error("Refused control connection");
814 close(client_fd);
815 buffer_free(&m);
816 return;
817 }
818
Darren Tucker7ebfc102004-11-07 20:06:19 +1100819 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000820 if (ssh_msg_recv(client_fd, &m) == -1) {
821 error("%s: client msg_recv failed", __func__);
822 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000823 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000824 return;
825 }
Damien Miller13390022005-07-06 09:44:19 +1000826 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Damien Miller0e220db2004-06-15 10:34:08 +1000827 error("%s: wrong client version %d", __func__, ver);
828 buffer_free(&m);
829 close(client_fd);
830 return;
831 }
832
Damien Miller07d86be2006-03-26 14:19:21 +1100833 cctx = xcalloc(1, sizeof(*cctx));
Darren Tucker7ebfc102004-11-07 20:06:19 +1100834 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
835 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
Damien Miller13390022005-07-06 09:44:19 +1000836 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
837 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000838 cctx->term = buffer_get_string(&m, &len);
839
840 cmd = buffer_get_string(&m, &len);
841 buffer_init(&cctx->cmd);
842 buffer_append(&cctx->cmd, cmd, strlen(cmd));
843
Damien Miller3756dce2004-06-18 01:17:29 +1000844 env_len = buffer_get_int(&m);
845 env_len = MIN(env_len, 4096);
846 debug3("%s: receiving %d env vars", __func__, env_len);
847 if (env_len != 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100848 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
Damien Miller3756dce2004-06-18 01:17:29 +1000849 for (i = 0; i < env_len; i++)
850 cctx->env[i] = buffer_get_string(&m, &len);
851 cctx->env[i] = NULL;
852 }
853
Damien Miller0e220db2004-06-15 10:34:08 +1000854 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
855 cctx->want_tty, cctx->want_subsys, cmd);
Damien Miller24fd8dd2006-06-13 13:00:09 +1000856 xfree(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +1000857
858 /* Gather fds from client */
859 new_fd[0] = mm_receive_fd(client_fd);
860 new_fd[1] = mm_receive_fd(client_fd);
861 new_fd[2] = mm_receive_fd(client_fd);
862
863 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
864 new_fd[0], new_fd[1], new_fd[2]);
865
866 /* Try to pick up ttymodes from client before it goes raw */
867 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
868 error("%s: tcgetattr: %s", __func__, strerror(errno));
869
Darren Tucker7ebfc102004-11-07 20:06:19 +1100870 /* This roundtrip is just for synchronisation of ttymodes */
Damien Miller0e220db2004-06-15 10:34:08 +1000871 buffer_clear(&m);
Damien Miller13390022005-07-06 09:44:19 +1000872 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000873 error("%s: client msg_send failed", __func__);
874 close(client_fd);
875 close(new_fd[0]);
876 close(new_fd[1]);
877 close(new_fd[2]);
Damien Miller23f07702004-06-18 01:19:03 +1000878 buffer_free(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100879 xfree(cctx->term);
880 if (env_len != 0) {
881 for (i = 0; i < env_len; i++)
882 xfree(cctx->env[i]);
883 xfree(cctx->env);
884 }
Damien Miller0e220db2004-06-15 10:34:08 +1000885 return;
886 }
887 buffer_free(&m);
888
889 /* enable nonblocking unless tty */
890 if (!isatty(new_fd[0]))
891 set_nonblock(new_fd[0]);
892 if (!isatty(new_fd[1]))
893 set_nonblock(new_fd[1]);
894 if (!isatty(new_fd[2]))
895 set_nonblock(new_fd[2]);
896
897 set_nonblock(client_fd);
898
Darren Tuckerfc959702004-07-17 16:12:08 +1000899 c = channel_new("session", SSH_CHANNEL_OPENING,
Damien Miller0e220db2004-06-15 10:34:08 +1000900 new_fd[0], new_fd[1], new_fd[2],
901 CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT,
902 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
903
904 /* XXX */
905 c->ctl_fd = client_fd;
906
907 debug3("%s: channel_new: %d", __func__, c->self);
908
909 channel_send_open(c->self);
910 channel_register_confirm(c->self, client_extra_session2_setup, cctx);
911}
912
913static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000914process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000915{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000916 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100917 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000918 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000919 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100920 u_short cancel_port;
921 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000922
923 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000924 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000925 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000926 if (s == NULL)
927 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000928 while (*s && isspace(*s))
929 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000930 if (*s == '-')
931 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000932 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000933 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000934
Darren Tucker1973c882004-05-24 10:34:36 +1000935 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000936 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000937 logit(" -L[bind_address:]port:host:hostport "
938 "Request local forward");
939 logit(" -R[bind_address:]port:host:hostport "
940 "Request remote forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000941 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000942 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100943 if (!options.permit_local_command)
944 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000945 logit(" !args "
946 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100947 goto out;
948 }
949
950 if (*s == '!' && options.permit_local_command) {
951 s++;
952 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000953 goto out;
954 }
955
956 if (*s == 'K') {
957 delete = 1;
958 s++;
959 }
960 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +1000961 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000962 goto out;
963 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000964 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000965 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +1000966 if (local && delete) {
967 logit("Not supported.");
968 goto out;
969 }
970 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000971 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000972 goto out;
973 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000974
975 s++;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000976 while (*s && isspace(*s))
977 s++;
978
Darren Tuckere7066df2004-05-24 10:18:05 +1000979 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100980 cancel_port = 0;
981 cancel_host = hpdelim(&s); /* may be NULL */
982 if (s != NULL) {
983 cancel_port = a2port(s);
984 cancel_host = cleanhostname(cancel_host);
985 } else {
986 cancel_port = a2port(cancel_host);
987 cancel_host = NULL;
988 }
989 if (cancel_port == 0) {
990 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000991 goto out;
992 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100993 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000994 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100995 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000996 logit("Bad forwarding specification.");
997 goto out;
998 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000999 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001000 if (channel_setup_local_fwd_listener(fwd.listen_host,
1001 fwd.listen_port, fwd.connect_host,
1002 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +10001003 logit("Port forwarding failed.");
1004 goto out;
1005 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001006 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001007 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001008 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +10001009 fwd.connect_port) < 0) {
1010 logit("Port forwarding failed.");
1011 goto out;
1012 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001013 }
1014
Darren Tuckere7066df2004-05-24 10:18:05 +10001015 logit("Forwarding port.");
1016 }
1017
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001018out:
1019 signal(SIGINT, handler);
1020 enter_raw_mode();
1021 if (cmd)
1022 xfree(cmd);
1023}
1024
Damien Millerad833b32000-08-23 10:46:23 +10001025/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +00001026static int
Damien Millerad833b32000-08-23 10:46:23 +10001027process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1028{
1029 char string[1024];
1030 pid_t pid;
1031 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001032 u_int i;
1033 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001034 char *s;
1035
Damien Millereccb9de2005-06-17 12:59:34 +10001036 if (len <= 0)
1037 return (0);
1038
1039 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001040 /* Get one character at a time. */
1041 ch = buf[i];
1042
1043 if (escape_pending) {
1044 /* We have previously seen an escape character. */
1045 /* Clear the flag now. */
1046 escape_pending = 0;
1047
1048 /* Process the escaped character. */
1049 switch (ch) {
1050 case '.':
1051 /* Terminate the connection. */
1052 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1053 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001054
1055 quit_pending = 1;
1056 return -1;
1057
1058 case 'Z' - 64:
1059 /* Suspend the program. */
1060 /* Print a message to that effect to the user. */
1061 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1062 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001063
1064 /* Restore terminal modes and suspend. */
1065 client_suspend_self(bin, bout, berr);
1066
1067 /* We have been continued. */
1068 continue;
1069
Damien Miller54c45982003-05-15 10:20:13 +10001070 case 'B':
1071 if (compat20) {
1072 snprintf(string, sizeof string,
1073 "%cB\r\n", escape_char);
1074 buffer_append(berr, string,
1075 strlen(string));
1076 channel_request_start(session_ident,
1077 "break", 0);
1078 packet_put_int(1000);
1079 packet_send();
1080 }
1081 continue;
1082
Ben Lindstromf28f6342001-04-04 02:03:04 +00001083 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001084 if (compat20) {
1085 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +10001086 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001087 else
1088 need_rekeying = 1;
1089 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001090 continue;
1091
Damien Millerad833b32000-08-23 10:46:23 +10001092 case '&':
Damien Millerad833b32000-08-23 10:46:23 +10001093 /*
1094 * Detach the program (continue to serve connections,
1095 * but put in background and no more new connections).
1096 */
Damien Miller96507ef2001-11-12 10:52:25 +11001097 /* Restore tty modes. */
1098 leave_raw_mode();
1099
1100 /* Stop listening for new connections. */
1101 channel_stop_listening();
1102
1103 snprintf(string, sizeof string,
1104 "%c& [backgrounded]\n", escape_char);
1105 buffer_append(berr, string, strlen(string));
1106
1107 /* Fork into background. */
1108 pid = fork();
1109 if (pid < 0) {
1110 error("fork: %.100s", strerror(errno));
1111 continue;
1112 }
1113 if (pid != 0) { /* This is the parent. */
1114 /* The parent just exits. */
1115 exit(0);
1116 }
1117 /* The child continues serving connections. */
1118 if (compat20) {
1119 buffer_append(bin, "\004", 1);
1120 /* fake EOF on stdin */
1121 return -1;
1122 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001123 /*
1124 * Sending SSH_CMSG_EOF alone does not always appear
1125 * to be enough. So we try to send an EOF character
1126 * first.
1127 */
1128 packet_start(SSH_CMSG_STDIN_DATA);
1129 packet_put_string("\004", 1);
1130 packet_send();
1131 /* Close stdin. */
1132 stdin_eof = 1;
1133 if (buffer_len(bin) == 0) {
1134 packet_start(SSH_CMSG_EOF);
1135 packet_send();
1136 }
1137 }
Damien Miller96507ef2001-11-12 10:52:25 +11001138 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001139
1140 case '?':
1141 snprintf(string, sizeof string,
1142"%c?\r\n\
1143Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001144%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +10001145%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001146%cC - open a command line\r\n\
1147%cR - Request rekey (SSH protocol 2 only)\r\n\
1148%c^Z - suspend ssh\r\n\
1149%c# - list forwarded connections\r\n\
1150%c& - background ssh (when waiting for connections to terminate)\r\n\
1151%c? - this message\r\n\
1152%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001153(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +10001154 escape_char, escape_char, escape_char, escape_char,
1155 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +10001156 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001157 buffer_append(berr, string, strlen(string));
1158 continue;
1159
1160 case '#':
1161 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1162 buffer_append(berr, string, strlen(string));
1163 s = channel_open_message();
1164 buffer_append(berr, s, strlen(s));
1165 xfree(s);
1166 continue;
1167
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001168 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001169 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001170 continue;
1171
Damien Millerad833b32000-08-23 10:46:23 +10001172 default:
1173 if (ch != escape_char) {
1174 buffer_put_char(bin, escape_char);
1175 bytes++;
1176 }
1177 /* Escaped characters fall through here */
1178 break;
1179 }
1180 } else {
1181 /*
1182 * The previous character was not an escape char. Check if this
1183 * is an escape.
1184 */
1185 if (last_was_cr && ch == escape_char) {
1186 /* It is. Set the flag and continue to next character. */
1187 escape_pending = 1;
1188 continue;
1189 }
1190 }
1191
1192 /*
1193 * Normal character. Record whether it was a newline,
1194 * and append it to the buffer.
1195 */
1196 last_was_cr = (ch == '\r' || ch == '\n');
1197 buffer_put_char(bin, ch);
1198 bytes++;
1199 }
1200 return bytes;
1201}
1202
Ben Lindstrombba81212001-06-25 05:01:22 +00001203static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001204client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001205{
Damien Miller166fca82000-04-20 07:42:21 +10001206 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001207 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001208
Damien Miller95def091999-11-25 00:26:21 +11001209 /* Read input from stdin. */
1210 if (FD_ISSET(fileno(stdin), readset)) {
1211 /* Read as much as possible. */
1212 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001213 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1214 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001215 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001216 /*
1217 * Received EOF or error. They are treated
1218 * similarly, except that an error message is printed
1219 * if it was an error condition.
1220 */
Damien Miller95def091999-11-25 00:26:21 +11001221 if (len < 0) {
1222 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1223 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001224 }
1225 /* Mark that we have seen EOF. */
1226 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001227 /*
1228 * Send an EOF message to the server unless there is
1229 * data in the buffer. If there is data in the
1230 * buffer, no message will be sent now. Code
1231 * elsewhere will send the EOF when the buffer
1232 * becomes empty if stdin_eof is set.
1233 */
Damien Miller95def091999-11-25 00:26:21 +11001234 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235 packet_start(SSH_CMSG_EOF);
1236 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001237 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001238 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001239 /*
1240 * Normal successful read, and no escape character.
1241 * Just append the data to buffer.
1242 */
Damien Miller95def091999-11-25 00:26:21 +11001243 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001244 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001245 /*
1246 * Normal, successful read. But we have an escape character
1247 * and have to process the characters one by one.
1248 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +00001249 if (process_escapes(&stdin_buffer, &stdout_buffer,
1250 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001251 return;
Damien Miller95def091999-11-25 00:26:21 +11001252 }
1253 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
1255
Ben Lindstrombba81212001-06-25 05:01:22 +00001256static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001257client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258{
Damien Miller95def091999-11-25 00:26:21 +11001259 int len;
1260 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261
Damien Miller95def091999-11-25 00:26:21 +11001262 /* Write buffered output to stdout. */
1263 if (FD_ISSET(fileno(stdout), writeset)) {
1264 /* Write as much data as possible. */
1265 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001266 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001267 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001268 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001269 len = 0;
1270 else {
Damien Miller5428f641999-11-25 11:54:57 +11001271 /*
1272 * An error or EOF was encountered. Put an
1273 * error message to stderr buffer.
1274 */
Damien Miller95def091999-11-25 00:26:21 +11001275 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1276 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001277 quit_pending = 1;
1278 return;
1279 }
1280 }
1281 /* Consume printed data from the buffer. */
1282 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001283 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284 }
Damien Miller95def091999-11-25 00:26:21 +11001285 /* Write buffered output to stderr. */
1286 if (FD_ISSET(fileno(stderr), writeset)) {
1287 /* Write as much data as possible. */
1288 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001289 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001290 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001291 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001292 len = 0;
1293 else {
Damien Miller5428f641999-11-25 11:54:57 +11001294 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001295 quit_pending = 1;
1296 return;
1297 }
1298 }
1299 /* Consume printed characters from the buffer. */
1300 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001301 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001302 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303}
1304
Damien Miller5428f641999-11-25 11:54:57 +11001305/*
Damien Millerb38eff82000-04-01 11:09:21 +10001306 * Get packets from the connection input buffer, and process them as long as
1307 * there are packets available.
1308 *
1309 * Any unknown packets received during the actual
1310 * session cause the session to terminate. This is
1311 * intended to make debugging easier since no
1312 * confirmations are sent. Any compatible protocol
1313 * extensions must be negotiated during the
1314 * preparatory phase.
1315 */
1316
Ben Lindstrombba81212001-06-25 05:01:22 +00001317static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001318client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001319{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001320 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001321}
1322
Damien Millerad833b32000-08-23 10:46:23 +10001323/* scan buf[] for '~' before sending data to the peer */
1324
Ben Lindstrombba81212001-06-25 05:01:22 +00001325static int
Damien Millerad833b32000-08-23 10:46:23 +10001326simple_escape_filter(Channel *c, char *buf, int len)
1327{
1328 /* XXX we assume c->extended is writeable */
1329 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1330}
1331
Ben Lindstrombba81212001-06-25 05:01:22 +00001332static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001333client_channel_closed(int id, void *arg)
1334{
Damien Miller3ec27592001-10-12 11:35:04 +10001335 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001336 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001337 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001338}
1339
Damien Millerb38eff82000-04-01 11:09:21 +10001340/*
Damien Miller5428f641999-11-25 11:54:57 +11001341 * Implements the interactive session with the server. This is called after
1342 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001343 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1344 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001345 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346
Damien Miller4af51302000-04-16 11:18:38 +10001347int
Damien Millerad833b32000-08-23 10:46:23 +10001348client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349{
Damien Miller5e953212001-01-30 09:14:00 +11001350 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001351 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001352 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1353 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001354 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Damien Miller95def091999-11-25 00:26:21 +11001356 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
Damien Miller95def091999-11-25 00:26:21 +11001358 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359
Damien Miller95def091999-11-25 00:26:21 +11001360 /* Initialize variables. */
1361 escape_pending = 0;
1362 last_was_cr = 1;
1363 exit_status = -1;
1364 stdin_eof = 0;
1365 buffer_high = 64 * 1024;
1366 connection_in = packet_get_connection_in();
1367 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001368 max_fd = MAX(connection_in, connection_out);
Damien Miller0e220db2004-06-15 10:34:08 +10001369 if (control_fd != -1)
1370 max_fd = MAX(max_fd, control_fd);
Damien Miller5e953212001-01-30 09:14:00 +11001371
1372 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001373 /* enable nonblocking unless tty */
1374 if (!isatty(fileno(stdin)))
1375 set_nonblock(fileno(stdin));
1376 if (!isatty(fileno(stdout)))
1377 set_nonblock(fileno(stdout));
1378 if (!isatty(fileno(stderr)))
1379 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001380 max_fd = MAX(max_fd, fileno(stdin));
1381 max_fd = MAX(max_fd, fileno(stdout));
1382 max_fd = MAX(max_fd, fileno(stderr));
1383 }
Damien Miller95def091999-11-25 00:26:21 +11001384 stdin_bytes = 0;
1385 stdout_bytes = 0;
1386 stderr_bytes = 0;
1387 quit_pending = 0;
1388 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Damien Miller95def091999-11-25 00:26:21 +11001390 /* Initialize buffers. */
1391 buffer_init(&stdin_buffer);
1392 buffer_init(&stdout_buffer);
1393 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394
Damien Millerb38eff82000-04-01 11:09:21 +10001395 client_init_dispatch();
1396
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001397 /*
1398 * Set signal handlers, (e.g. to restore non-blocking mode)
1399 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1400 */
Darren Tucker07336da2004-11-05 20:02:16 +11001401 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1402 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001403 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1404 signal(SIGINT, signal_handler);
1405 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1406 signal(SIGQUIT, signal_handler);
1407 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1408 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001409 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Damien Miller95def091999-11-25 00:26:21 +11001411 if (have_pty)
1412 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413
Ben Lindstrom5b828322001-02-09 01:34:36 +00001414 if (compat20) {
1415 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001416 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001417 channel_register_filter(session_ident,
Damien Miller077b2382005-12-31 16:22:32 +11001418 simple_escape_filter, NULL);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001419 if (session_ident != -1)
1420 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001421 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001422 } else {
1423 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001424 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001425 }
Damien Millerad833b32000-08-23 10:46:23 +10001426
Damien Miller95def091999-11-25 00:26:21 +11001427 /* Main loop of the client for the interactive session mode. */
1428 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Miller5428f641999-11-25 11:54:57 +11001430 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001431 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001433 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001434 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001435
1436 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001437
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001438 if (rekeying) {
1439 debug("rekeying in progress");
1440 } else {
1441 /*
1442 * Make packets of buffered stdin data, and buffer
1443 * them for sending to the server.
1444 */
1445 if (!compat20)
1446 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001448 /*
1449 * Make packets from buffered channel data, and
1450 * enqueue them for sending to the server.
1451 */
1452 if (packet_not_very_much_data_to_write())
1453 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001455 /*
1456 * Check if the window size has changed, and buffer a
1457 * message about it to the server if so.
1458 */
1459 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001461 if (quit_pending)
1462 break;
1463 }
Damien Miller5428f641999-11-25 11:54:57 +11001464 /*
1465 * Wait until we have something to do (something becomes
1466 * available on one of the descriptors).
1467 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001468 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001469 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001470 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001471
Damien Miller95def091999-11-25 00:26:21 +11001472 if (quit_pending)
1473 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001475 /* Do channel operations unless rekeying in progress. */
1476 if (!rekeying) {
1477 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001478 if (need_rekeying || packet_need_rekeying()) {
1479 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001480 xxx_kex->done = 0;
1481 kex_send_kexinit(xxx_kex);
1482 need_rekeying = 0;
1483 }
1484 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485
Damien Miller1383bd82000-04-06 12:32:37 +10001486 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001487 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Damien Miller0e220db2004-06-15 10:34:08 +10001489 /* Accept control connections. */
1490 client_process_control(readset);
1491
Damien Miller1383bd82000-04-06 12:32:37 +10001492 if (quit_pending)
1493 break;
1494
1495 if (!compat20) {
1496 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001497 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001498 /*
1499 * Process output to stdout and stderr. Output to
1500 * the connection is processed elsewhere (above).
1501 */
Damien Miller5e953212001-01-30 09:14:00 +11001502 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001503 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504
Damien Miller5428f641999-11-25 11:54:57 +11001505 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001506 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001507 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508 }
Damien Miller5e953212001-01-30 09:14:00 +11001509 if (readset)
1510 xfree(readset);
1511 if (writeset)
1512 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513
Damien Miller95def091999-11-25 00:26:21 +11001514 /* Terminate the session. */
1515
1516 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001517 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001518
Ben Lindstrom601e4362001-06-21 03:19:23 +00001519 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001520
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001521 if (have_pty)
1522 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001523
1524 /* restore blocking io */
1525 if (!isatty(fileno(stdin)))
1526 unset_nonblock(fileno(stdin));
1527 if (!isatty(fileno(stdout)))
1528 unset_nonblock(fileno(stdout));
1529 if (!isatty(fileno(stderr)))
1530 unset_nonblock(fileno(stderr));
1531
Damien Millerd6965512003-12-17 16:31:53 +11001532 /*
1533 * If there was no shell or command requested, there will be no remote
1534 * exit status to be returned. In that case, clear error code if the
1535 * connection was deliberately terminated at this end.
1536 */
1537 if (no_shell_flag && received_signal == SIGTERM) {
1538 received_signal = 0;
1539 exit_status = 0;
1540 }
1541
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001542 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001543 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001544
1545 /*
1546 * In interactive mode (with pseudo tty) display a message indicating
1547 * that the connection has been closed.
1548 */
1549 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1550 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1551 buffer_append(&stderr_buffer, buf, strlen(buf));
1552 }
1553
Damien Miller95def091999-11-25 00:26:21 +11001554 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001555 while (buffer_len(&stdout_buffer) > 0) {
1556 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001557 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001558 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001559 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001560 break;
1561 }
Damien Miller95def091999-11-25 00:26:21 +11001562 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001563 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565
Damien Miller95def091999-11-25 00:26:21 +11001566 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001567 while (buffer_len(&stderr_buffer) > 0) {
1568 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001569 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001570 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001571 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001572 break;
1573 }
Damien Miller95def091999-11-25 00:26:21 +11001574 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001575 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001576 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577
Damien Miller95def091999-11-25 00:26:21 +11001578 /* Clear and free any buffers. */
1579 memset(buf, 0, sizeof(buf));
1580 buffer_free(&stdin_buffer);
1581 buffer_free(&stdout_buffer);
1582 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller95def091999-11-25 00:26:21 +11001584 /* Report bytes transferred, and transfer rates. */
1585 total_time = get_current_time() - start_time;
1586 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001587 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001588 if (total_time > 0)
1589 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001590 stdin_bytes / total_time, stdout_bytes / total_time,
1591 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001592
1593 /* Return the exit status of the program. */
1594 debug("Exit status %d", exit_status);
1595 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596}
Damien Millerb38eff82000-04-01 11:09:21 +10001597
1598/*********/
1599
Ben Lindstrombba81212001-06-25 05:01:22 +00001600static void
Damien Miller630d6f42002-01-22 23:17:30 +11001601client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001602{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001603 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001604 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001605 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001606 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001607 memset(data, 0, data_len);
1608 xfree(data);
1609}
Ben Lindstrombba81212001-06-25 05:01:22 +00001610static void
Damien Miller630d6f42002-01-22 23:17:30 +11001611client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001612{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001613 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001614 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001615 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001616 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001617 memset(data, 0, data_len);
1618 xfree(data);
1619}
Ben Lindstrombba81212001-06-25 05:01:22 +00001620static void
Damien Miller630d6f42002-01-22 23:17:30 +11001621client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001622{
Damien Millerb38eff82000-04-01 11:09:21 +10001623 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001624 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001625 /* Acknowledge the exit. */
1626 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1627 packet_send();
1628 /*
1629 * Must wait for packet to be sent since we are
1630 * exiting the loop.
1631 */
1632 packet_write_wait();
1633 /* Flag that we want to exit. */
1634 quit_pending = 1;
1635}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001636static void
1637client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1638{
1639 Channel *c = NULL;
1640 int remote_id, sock;
1641
1642 /* Read the remote channel number from the message. */
1643 remote_id = packet_get_int();
1644 packet_check_eom();
1645
1646 /*
1647 * Get a connection to the local authentication agent (this may again
1648 * get forwarded).
1649 */
1650 sock = ssh_get_authentication_socket();
1651
1652 /*
1653 * If we could not connect the agent, send an error message back to
1654 * the server. This should never happen unless the agent dies,
1655 * because authentication forwarding is only enabled if we have an
1656 * agent.
1657 */
1658 if (sock >= 0) {
1659 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1660 -1, 0, 0, 0, "authentication agent connection", 1);
1661 c->remote_id = remote_id;
1662 c->force_drain = 1;
1663 }
1664 if (c == NULL) {
1665 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1666 packet_put_int(remote_id);
1667 } else {
1668 /* Send a confirmation to the remote host. */
1669 debug("Forwarding authentication connection.");
1670 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1671 packet_put_int(remote_id);
1672 packet_put_int(c->self);
1673 }
1674 packet_send();
1675}
Damien Millerb38eff82000-04-01 11:09:21 +10001676
Ben Lindstrombba81212001-06-25 05:01:22 +00001677static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001678client_request_forwarded_tcpip(const char *request_type, int rchan)
1679{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001680 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001681 char *listen_address, *originator_address;
1682 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001683 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001684
1685 /* Get rest of the packet */
1686 listen_address = packet_get_string(NULL);
1687 listen_port = packet_get_int();
1688 originator_address = packet_get_string(NULL);
1689 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001690 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001691
1692 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1693 listen_address, listen_port, originator_address, originator_port);
1694
Ben Lindstrom173e6462001-07-04 05:15:15 +00001695 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001696 if (sock < 0) {
1697 xfree(originator_address);
1698 xfree(listen_address);
1699 return NULL;
1700 }
1701 c = channel_new("forwarded-tcpip",
1702 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1703 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001704 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001705 xfree(originator_address);
1706 xfree(listen_address);
1707 return c;
1708}
1709
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001710static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001711client_request_x11(const char *request_type, int rchan)
1712{
1713 Channel *c = NULL;
1714 char *originator;
1715 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001716 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001717
1718 if (!options.forward_x11) {
1719 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001720 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001721 return NULL;
1722 }
1723 originator = packet_get_string(NULL);
1724 if (datafellows & SSH_BUG_X11FWD) {
1725 debug2("buggy server: x11 request w/o originator_port");
1726 originator_port = 0;
1727 } else {
1728 originator_port = packet_get_int();
1729 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001730 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001731 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001732 debug("client_request_x11: request from %s %d", originator,
1733 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001734 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001735 sock = x11_connect_display();
1736 if (sock < 0)
1737 return NULL;
1738 c = channel_new("x11",
1739 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001740 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001741 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001742 return c;
1743}
1744
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001745static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001746client_request_agent(const char *request_type, int rchan)
1747{
1748 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001749 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001750
1751 if (!options.forward_agent) {
1752 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001753 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001754 return NULL;
1755 }
1756 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001757 if (sock < 0)
1758 return NULL;
1759 c = channel_new("authentication agent connection",
1760 SSH_CHANNEL_OPEN, sock, sock, -1,
1761 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001762 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001763 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001764 return c;
1765}
1766
Damien Millerbd483e72000-04-30 10:00:53 +10001767/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001768static void
Damien Miller630d6f42002-01-22 23:17:30 +11001769client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001770{
1771 Channel *c = NULL;
1772 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001773 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001774 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001775
1776 ctype = packet_get_string(&len);
1777 rchan = packet_get_int();
1778 rwindow = packet_get_int();
1779 rmaxpack = packet_get_int();
1780
Damien Millere247cc42000-05-07 12:03:14 +10001781 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001782 ctype, rchan, rwindow, rmaxpack);
1783
Damien Miller0bc1bd82000-11-13 22:57:25 +11001784 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1785 c = client_request_forwarded_tcpip(ctype, rchan);
1786 } else if (strcmp(ctype, "x11") == 0) {
1787 c = client_request_x11(ctype, rchan);
1788 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1789 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001790 }
1791/* XXX duplicate : */
1792 if (c != NULL) {
1793 debug("confirm %s", ctype);
1794 c->remote_id = rchan;
1795 c->remote_window = rwindow;
1796 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001797 if (c->type != SSH_CHANNEL_CONNECTING) {
1798 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1799 packet_put_int(c->remote_id);
1800 packet_put_int(c->self);
1801 packet_put_int(c->local_window);
1802 packet_put_int(c->local_maxpacket);
1803 packet_send();
1804 }
Damien Millerbd483e72000-04-30 10:00:53 +10001805 } else {
1806 debug("failure %s", ctype);
1807 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1808 packet_put_int(rchan);
1809 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001810 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001811 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001812 packet_put_cstring("");
1813 }
Damien Millerbd483e72000-04-30 10:00:53 +10001814 packet_send();
1815 }
1816 xfree(ctype);
1817}
Ben Lindstrombba81212001-06-25 05:01:22 +00001818static void
Damien Miller630d6f42002-01-22 23:17:30 +11001819client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001820{
1821 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001822 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001823 char *rtype;
1824
1825 id = packet_get_int();
1826 rtype = packet_get_string(NULL);
1827 reply = packet_get_char();
1828
1829 debug("client_input_channel_req: channel %d rtype %s reply %d",
1830 id, rtype, reply);
1831
Damien Miller3bbd8782004-06-18 22:23:22 +10001832 if (id == -1) {
1833 error("client_input_channel_req: request for channel -1");
1834 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001835 error("client_input_channel_req: channel %d: unknown channel", id);
1836 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001837 exitval = packet_get_int();
1838 if (id == session_ident) {
1839 success = 1;
1840 exit_status = exitval;
1841 } else if (c->ctl_fd == -1) {
1842 error("client_input_channel_req: unexpected channel %d",
1843 session_ident);
1844 } else {
1845 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1846 success = 1;
1847 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001848 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001849 }
1850 if (reply) {
1851 packet_start(success ?
1852 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001853 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001854 packet_send();
1855 }
1856 xfree(rtype);
1857}
Damien Millerc3fa4072002-01-22 23:21:58 +11001858static void
1859client_input_global_request(int type, u_int32_t seq, void *ctxt)
1860{
1861 char *rtype;
1862 int want_reply;
1863 int success = 0;
1864
1865 rtype = packet_get_string(NULL);
1866 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001867 debug("client_input_global_request: rtype %s want_reply %d",
1868 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001869 if (want_reply) {
1870 packet_start(success ?
1871 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1872 packet_send();
1873 packet_write_wait();
1874 }
1875 xfree(rtype);
1876}
Damien Millerbd483e72000-04-30 10:00:53 +10001877
Damien Miller0e220db2004-06-15 10:34:08 +10001878void
Darren Tuckerfc959702004-07-17 16:12:08 +10001879client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller3756dce2004-06-18 01:17:29 +10001880 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
Damien Miller0e220db2004-06-15 10:34:08 +10001881 dispatch_fn *subsys_repl)
1882{
1883 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001884 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001885
1886 debug2("%s: id %d", __func__, id);
1887
Darren Tucker5d78de62004-11-05 20:35:44 +11001888 if ((c = channel_lookup(id)) == NULL)
1889 fatal("client_session2_setup: channel %d: unknown channel", id);
1890
Damien Miller0e220db2004-06-15 10:34:08 +10001891 if (want_tty) {
1892 struct winsize ws;
1893 struct termios tio;
1894
1895 /* Store window size in the packet. */
1896 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1897 memset(&ws, 0, sizeof(ws));
1898
1899 channel_request_start(id, "pty-req", 0);
1900 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001901 packet_put_int((u_int)ws.ws_col);
1902 packet_put_int((u_int)ws.ws_row);
1903 packet_put_int((u_int)ws.ws_xpixel);
1904 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10001905 tio = get_saved_tio();
1906 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
1907 packet_send();
1908 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001909 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001910 }
1911
1912 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001913 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001914 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001915 char *name, *val;
1916
1917 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001918 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001919 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001920 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001921 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001922 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001923 continue;
1924 }
1925 *val++ = '\0';
1926
1927 matched = 0;
1928 for (j = 0; j < options.num_send_env; j++) {
1929 if (match_pattern(name, options.send_env[j])) {
1930 matched = 1;
1931 break;
1932 }
1933 }
1934 if (!matched) {
1935 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001936 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001937 continue;
1938 }
1939
1940 debug("Sending env %s = %s", name, val);
1941 channel_request_start(id, "env", 0);
1942 packet_put_cstring(name);
1943 packet_put_cstring(val);
1944 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001945 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001946 }
1947 }
1948
1949 len = buffer_len(cmd);
1950 if (len > 0) {
1951 if (len > 900)
1952 len = 900;
1953 if (want_subsystem) {
1954 debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
1955 channel_request_start(id, "subsystem", subsys_repl != NULL);
1956 if (subsys_repl != NULL) {
1957 /* register callback for reply */
1958 /* XXX we assume that client_loop has already been called */
1959 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
1960 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
1961 }
1962 } else {
1963 debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
1964 channel_request_start(id, "exec", 0);
1965 }
1966 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1967 packet_send();
1968 } else {
1969 channel_request_start(id, "shell", 0);
1970 packet_send();
1971 }
1972}
1973
Ben Lindstrombba81212001-06-25 05:01:22 +00001974static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001975client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001976{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001977 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001978
Damien Miller1383bd82000-04-06 12:32:37 +10001979 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1980 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1981 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1982 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001983 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001984 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1985 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001986 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001987 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001988 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001989
1990 /* rekeying */
1991 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001992
1993 /* global request reply messages */
1994 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1995 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001996}
Ben Lindstrombba81212001-06-25 05:01:22 +00001997static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001998client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001999{
2000 dispatch_init(NULL);
2001 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2002 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2003 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002004 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2005 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2006 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002007 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2008 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2009 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002010
2011 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002012 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002013 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2014 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002015}
Ben Lindstrombba81212001-06-25 05:01:22 +00002016static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002017client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002018{
2019 client_init_dispatch_13();
2020 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2021 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2022}
Ben Lindstromdb47f382001-07-04 05:10:27 +00002023static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002024client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002025{
Damien Miller1383bd82000-04-06 12:32:37 +10002026 if (compat20)
2027 client_init_dispatch_20();
2028 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002029 client_init_dispatch_13();
2030 else
2031 client_init_dispatch_15();
2032}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002033
2034/* client specific fatal cleanup */
2035void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002036cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002037{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002038 leave_raw_mode();
2039 leave_non_blocking();
Damien Miller0e220db2004-06-15 10:34:08 +10002040 if (options.control_path != NULL && control_fd != -1)
2041 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002042 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002043}