blob: 52f3bd5b55c4be38633da24ac23cc9d96c5589a0 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 *
14 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
Damien Miller1383bd82000-04-06 12:32:37 +100037 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000038 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110039 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110059 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110062RCSID("$OpenBSD: clientloop.c,v 1.152 2006/02/10 00:27:13 stevesk Exp $");
63
64#include <sys/ioctl.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110065
Damien Miller03e20032006-03-15 11:16:59 +110066#include <paths.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110067#include <termios.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "ssh1.h"
71#include "ssh2.h"
72#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "packet.h"
74#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100075#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000076#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100077#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100078#include "buffer.h"
79#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110080#include "key.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +000081#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "log.h"
83#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000084#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +110085#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "authfd.h"
87#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100088#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +000089#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +100090#include "monitor_fdpass.h"
91#include "match.h"
92#include "msg.h"
Damien Miller69b69aa2000-10-28 14:19:58 +110093
94/* import options */
95extern Options options;
96
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097/* Flag indicating that stdin should be redirected from /dev/null. */
98extern int stdin_null_flag;
99
Damien Millerd6965512003-12-17 16:31:53 +1100100/* Flag indicating that no shell has been requested */
101extern int no_shell_flag;
102
Damien Miller0e220db2004-06-15 10:34:08 +1000103/* Control socket */
104extern int control_fd;
105
Damien Miller5428f641999-11-25 11:54:57 +1100106/*
107 * Name of the host we are connecting to. This is the name given on the
108 * command line, or the HostName specified for the user-supplied name in a
109 * configuration file.
110 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111extern char *host;
112
Damien Miller5428f641999-11-25 11:54:57 +1100113/*
114 * Flag to indicate that we have received a window change signal which has
115 * not yet been processed. This will cause a message indicating the new
116 * window size to be sent to the server a little later. This is volatile
117 * because this is updated in a signal handler.
118 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000119static volatile sig_atomic_t received_window_change_signal = 0;
120static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Damien Miller788f2122005-11-05 15:14:59 +1100122/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123static int in_non_blocking_mode = 0;
124
125/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000126static int quit_pending; /* Set to non-zero to quit the client loop. */
127static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100128static int escape_pending; /* Last character was the escape character */
129static int last_was_cr; /* Last character was a newline. */
130static int exit_status; /* Used to store the exit status of the command. */
131static int stdin_eof; /* EOF has been encountered on standard error. */
132static Buffer stdin_buffer; /* Buffer for stdin data. */
133static Buffer stdout_buffer; /* Buffer for stdout data. */
134static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000135static u_long stdin_bytes, stdout_bytes, stderr_bytes;
136static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100137static int connection_in; /* Connection to server (input). */
138static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000139static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000140static int session_closed = 0; /* In SSH2: login session closed. */
Damien Miller509b0102003-12-17 16:33:10 +1100141static int server_alive_timeouts = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Ben Lindstrombba81212001-06-25 05:01:22 +0000143static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000144int session_ident = -1;
145
Damien Miller0e220db2004-06-15 10:34:08 +1000146struct confirm_ctx {
147 int want_tty;
148 int want_subsys;
Damien Miller13390022005-07-06 09:44:19 +1000149 int want_x_fwd;
150 int want_agent_fwd;
Damien Miller0e220db2004-06-15 10:34:08 +1000151 Buffer cmd;
152 char *term;
153 struct termios tio;
Damien Miller3756dce2004-06-18 01:17:29 +1000154 char **env;
Damien Miller0e220db2004-06-15 10:34:08 +1000155};
156
Ben Lindstromf28f6342001-04-04 02:03:04 +0000157/*XXX*/
158extern Kex *xxx_kex;
159
Damien Miller0e220db2004-06-15 10:34:08 +1000160void ssh_process_session2_setup(int, int, int, Buffer *);
161
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162/* Restores stdin to blocking mode. */
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000165leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166{
Damien Miller95def091999-11-25 00:26:21 +1100167 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000168 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100169 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100170 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171}
172
Damien Miller95def091999-11-25 00:26:21 +1100173/* Puts stdin terminal in non-blocking mode. */
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000176enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
Damien Miller95def091999-11-25 00:26:21 +1100178 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000179 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Damien Miller5428f641999-11-25 11:54:57 +1100182/*
183 * Signal handler for the window change signal (SIGWINCH). This just sets a
184 * flag indicating that the window has changed.
185 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void
Damien Miller95def091999-11-25 00:26:21 +1100188window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189{
Damien Miller95def091999-11-25 00:26:21 +1100190 received_window_change_signal = 1;
191 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Miller5428f641999-11-25 11:54:57 +1100194/*
195 * Signal handler for signals that cause the program to terminate. These
196 * signals must be trapped to restore terminal modes.
197 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Damien Miller95def091999-11-25 00:26:21 +1100200signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000202 received_signal = sig;
203 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204}
205
Damien Miller5428f641999-11-25 11:54:57 +1100206/*
207 * Returns current time in seconds from Jan 1, 1970 with the maximum
208 * available resolution.
209 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000212get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 struct timeval tv;
215 gettimeofday(&tv, NULL);
216 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller17e7ed02005-06-17 12:54:33 +1000219#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
220void
221client_x11_get_proto(const char *display, const char *xauth_path,
222 u_int trusted, char **_proto, char **_data)
223{
224 char cmd[1024];
225 char line[512];
226 char xdisplay[512];
227 static char proto[512], data[512];
228 FILE *f;
229 int got_data = 0, generated = 0, do_unlink = 0, i;
230 char *xauthdir, *xauthfile;
231 struct stat st;
232
233 xauthdir = xauthfile = NULL;
234 *_proto = proto;
235 *_data = data;
236 proto[0] = data[0] = '\0';
237
238 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
239 debug("No xauth program.");
240 } else {
241 if (display == NULL) {
242 debug("x11_get_proto: DISPLAY not set");
243 return;
244 }
245 /*
246 * Handle FamilyLocal case where $DISPLAY does
247 * not match an authorization entry. For this we
248 * just try "xauth list unix:displaynum.screennum".
249 * XXX: "localhost" match to determine FamilyLocal
250 * is not perfect.
251 */
252 if (strncmp(display, "localhost:", 10) == 0) {
253 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
254 display + 10);
255 display = xdisplay;
256 }
257 if (trusted == 0) {
258 xauthdir = xmalloc(MAXPATHLEN);
259 xauthfile = xmalloc(MAXPATHLEN);
260 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
261 if (mkdtemp(xauthdir) != NULL) {
262 do_unlink = 1;
263 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
264 xauthdir);
265 snprintf(cmd, sizeof(cmd),
266 "%s -f %s generate %s " SSH_X11_PROTO
267 " untrusted timeout 1200 2>" _PATH_DEVNULL,
268 xauth_path, xauthfile, display);
269 debug2("x11_get_proto: %s", cmd);
270 if (system(cmd) == 0)
271 generated = 1;
272 }
273 }
274 snprintf(cmd, sizeof(cmd),
Darren Tuckerd89dbf22005-10-03 18:05:26 +1000275 "%s %s%s list %s 2>" _PATH_DEVNULL,
Damien Miller17e7ed02005-06-17 12:54:33 +1000276 xauth_path,
277 generated ? "-f " : "" ,
278 generated ? xauthfile : "",
279 display);
280 debug2("x11_get_proto: %s", cmd);
281 f = popen(cmd, "r");
282 if (f && fgets(line, sizeof(line), f) &&
283 sscanf(line, "%*s %511s %511s", proto, data) == 2)
284 got_data = 1;
285 if (f)
286 pclose(f);
287 }
288
289 if (do_unlink) {
290 unlink(xauthfile);
291 rmdir(xauthdir);
292 }
293 if (xauthdir)
294 xfree(xauthdir);
295 if (xauthfile)
296 xfree(xauthfile);
297
298 /*
299 * If we didn't get authentication data, just make up some
300 * data. The forwarding code will check the validity of the
301 * response anyway, and substitute this data. The X11
302 * server, however, will ignore this fake data and use
303 * whatever authentication mechanisms it was using otherwise
304 * for the local connection.
305 */
306 if (!got_data) {
307 u_int32_t rnd = 0;
308
309 logit("Warning: No xauth data; "
310 "using fake authentication data for X11 forwarding.");
311 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
312 for (i = 0; i < 16; i++) {
313 if (i % 4 == 0)
314 rnd = arc4random();
315 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
316 rnd & 0xff);
317 rnd >>= 8;
318 }
319 }
320}
321
Damien Miller5428f641999-11-25 11:54:57 +1100322/*
323 * This is called when the interactive is entered. This checks if there is
324 * an EOF coming on stdin. We must check this explicitly, as select() does
325 * not appear to wake up when redirecting from /dev/null.
326 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000329client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330{
Damien Miller95def091999-11-25 00:26:21 +1100331 int len;
332 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Miller5428f641999-11-25 11:54:57 +1100334 /*
335 * If standard input is to be "redirected from /dev/null", we simply
336 * mark that we have seen an EOF and send an EOF message to the
337 * server. Otherwise, we try to read a single character; it appears
338 * that for some files, such /dev/null, select() never wakes up for
339 * read for this descriptor, which means that we never get EOF. This
340 * way we will get the EOF if stdin comes from /dev/null or similar.
341 */
Damien Miller95def091999-11-25 00:26:21 +1100342 if (stdin_null_flag) {
343 /* Fake EOF on stdin. */
344 debug("Sending eof.");
345 stdin_eof = 1;
346 packet_start(SSH_CMSG_EOF);
347 packet_send();
348 } else {
Damien Miller95def091999-11-25 00:26:21 +1100349 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 /* Check for immediate EOF on stdin. */
352 len = read(fileno(stdin), buf, 1);
353 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100354 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100355 debug("Sending eof.");
356 stdin_eof = 1;
357 packet_start(SSH_CMSG_EOF);
358 packet_send();
359 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100360 /*
361 * Got data. We must store the data in the buffer,
362 * and also process it as an escape character if
363 * appropriate.
364 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000365 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100366 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000367 else
Damien Miller95def091999-11-25 00:26:21 +1100368 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100369 }
Damien Miller95def091999-11-25 00:26:21 +1100370 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372}
373
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Damien Miller5428f641999-11-25 11:54:57 +1100375/*
376 * Make packets from buffered stdin data, and buffer them for sending to the
377 * connection.
378 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000381client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000383 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller95def091999-11-25 00:26:21 +1100385 /* Send buffered stdin data to the server. */
386 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100387 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100388 len = buffer_len(&stdin_buffer);
389 /* Keep the packets at reasonable size. */
390 if (len > packet_get_maxsize())
391 len = packet_get_maxsize();
392 packet_start(SSH_CMSG_STDIN_DATA);
393 packet_put_string(buffer_ptr(&stdin_buffer), len);
394 packet_send();
395 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000396 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100397 /* If we have a pending EOF, send it now. */
398 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
399 packet_start(SSH_CMSG_EOF);
400 packet_send();
401 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403}
404
Damien Miller5428f641999-11-25 11:54:57 +1100405/*
406 * Checks if the client window has changed, and sends a packet about it to
407 * the server if so. The actual change is detected elsewhere (by a software
408 * interrupt on Unix); this just checks the flag and sends a message if
409 * appropriate.
410 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000413client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414{
Damien Miller1383bd82000-04-06 12:32:37 +1000415 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller1383bd82000-04-06 12:32:37 +1000417 if (! received_window_change_signal)
418 return;
419 /** XXX race */
420 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Millerd3444942000-09-30 14:20:03 +1100422 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000423
424 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000425 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000426 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000427 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
428 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000429 packet_start(SSH_CMSG_WINDOW_SIZE);
430 packet_put_int(ws.ws_row);
431 packet_put_int(ws.ws_col);
432 packet_put_int(ws.ws_xpixel);
433 packet_put_int(ws.ws_ypixel);
434 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436}
437
Damien Miller509b0102003-12-17 16:33:10 +1100438static void
439client_global_request_reply(int type, u_int32_t seq, void *ctxt)
440{
441 server_alive_timeouts = 0;
442 client_global_request_reply_fwd(type, seq, ctxt);
443}
444
445static void
446server_alive_check(void)
447{
448 if (++server_alive_timeouts > options.server_alive_count_max)
449 packet_disconnect("Timeout, server not responding.");
450 packet_start(SSH2_MSG_GLOBAL_REQUEST);
451 packet_put_cstring("keepalive@openssh.com");
452 packet_put_char(1); /* boolean: want reply */
453 packet_send();
454}
455
Damien Miller5428f641999-11-25 11:54:57 +1100456/*
457 * Waits until the client can do something (some data becomes available on
458 * one of the file descriptors).
459 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000460static void
Damien Miller5e953212001-01-30 09:14:00 +1100461client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000462 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463{
Damien Miller509b0102003-12-17 16:33:10 +1100464 struct timeval tv, *tvp;
465 int ret;
466
Damien Miller5e953212001-01-30 09:14:00 +1100467 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000468 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller1383bd82000-04-06 12:32:37 +1000470 if (!compat20) {
471 /* Read from the connection, unless our buffers are full. */
472 if (buffer_len(&stdout_buffer) < buffer_high &&
473 buffer_len(&stderr_buffer) < buffer_high &&
474 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100475 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000476 /*
477 * Read from stdin, unless we have seen EOF or have very much
478 * buffered data to send to the server.
479 */
480 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100481 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000482
483 /* Select stdout/stderr if have data in buffer. */
484 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100485 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000486 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100487 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000488 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000489 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000490 if (session_closed && !channel_still_open() &&
491 !packet_have_data_to_write()) {
492 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100493 memset(*readsetp, 0, *nallocp);
494 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000495 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000496 } else {
497 FD_SET(connection_in, *readsetp);
498 }
Damien Miller1383bd82000-04-06 12:32:37 +1000499 }
500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Select server connection if have data to write to the server. */
502 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100503 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100504
Damien Miller0e220db2004-06-15 10:34:08 +1000505 if (control_fd != -1)
506 FD_SET(control_fd, *readsetp);
507
Damien Miller5428f641999-11-25 11:54:57 +1100508 /*
509 * Wait for something to happen. This will suspend the process until
510 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100511 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100512 */
Damien Miller95def091999-11-25 00:26:21 +1100513
Damien Miller509b0102003-12-17 16:33:10 +1100514 if (options.server_alive_interval == 0 || !compat20)
515 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000516 else {
Damien Miller509b0102003-12-17 16:33:10 +1100517 tv.tv_sec = options.server_alive_interval;
518 tv.tv_usec = 0;
519 tvp = &tv;
520 }
521 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
522 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100523 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000524
525 /*
526 * We have to clear the select masks, because we return.
527 * We have to return, because the mainloop checks for the flags
528 * set by the signal handlers.
529 */
Damien Miller79faeff2001-11-12 11:06:32 +1100530 memset(*readsetp, 0, *nallocp);
531 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000532
Damien Miller95def091999-11-25 00:26:21 +1100533 if (errno == EINTR)
534 return;
535 /* Note: we might still have data in the buffers. */
536 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
537 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100538 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100539 } else if (ret == 0)
540 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541}
542
Ben Lindstrombba81212001-06-25 05:01:22 +0000543static void
Damien Millerad833b32000-08-23 10:46:23 +1000544client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545{
Damien Miller95def091999-11-25 00:26:21 +1100546 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000547 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000548 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000549 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000550 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551
Damien Miller95def091999-11-25 00:26:21 +1100552 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553
Damien Miller5428f641999-11-25 11:54:57 +1100554 /*
555 * Free (and clear) the buffer to reduce the amount of data that gets
556 * written to swap.
557 */
Damien Millerad833b32000-08-23 10:46:23 +1000558 buffer_free(bin);
559 buffer_free(bout);
560 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Send the suspend signal to the program itself. */
563 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
Darren Tucker5d78de62004-11-05 20:35:44 +1100565 /* Reset window sizes in case they have changed */
566 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
Damien Miller95def091999-11-25 00:26:21 +1100568 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000569 buffer_init(bin);
570 buffer_init(bout);
571 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574}
575
Ben Lindstrombba81212001-06-25 05:01:22 +0000576static void
Damien Miller1383bd82000-04-06 12:32:37 +1000577client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578{
Damien Miller1383bd82000-04-06 12:32:37 +1000579 int len;
580 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
Damien Miller5428f641999-11-25 11:54:57 +1100582 /*
583 * Read input from the server, and add any such data to the buffer of
584 * the packet subsystem.
585 */
Damien Miller95def091999-11-25 00:26:21 +1100586 if (FD_ISSET(connection_in, readset)) {
587 /* Read as much as possible. */
588 len = read(connection_in, buf, sizeof(buf));
589 if (len == 0) {
590 /* Received EOF. The remote host has closed the connection. */
591 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
592 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594 quit_pending = 1;
595 return;
Damien Miller95def091999-11-25 00:26:21 +1100596 }
Damien Miller5428f641999-11-25 11:54:57 +1100597 /*
598 * There is a kernel bug on Solaris that causes select to
599 * sometimes wake up even though there is no data available.
600 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000601 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100602 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 if (len < 0) {
605 /* An error has encountered. Perhaps there is a network problem. */
606 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
607 host, strerror(errno));
608 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100609 quit_pending = 1;
610 return;
611 }
612 packet_process_incoming(buf, len);
613 }
Damien Miller1383bd82000-04-06 12:32:37 +1000614}
615
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000616static void
Damien Miller0e220db2004-06-15 10:34:08 +1000617client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
618{
619 int id;
620 Channel *c;
Darren Tuckerfc959702004-07-17 16:12:08 +1000621
Damien Miller0e220db2004-06-15 10:34:08 +1000622 id = packet_get_int();
623 packet_check_eom();
624
625 if ((c = channel_lookup(id)) == NULL) {
626 error("%s: no channel for id %d", __func__, id);
627 return;
628 }
629
630 if (type == SSH2_MSG_CHANNEL_SUCCESS)
631 debug2("Request suceeded on channel %d", id);
632 else if (type == SSH2_MSG_CHANNEL_FAILURE) {
633 error("Request failed on channel %d", id);
634 channel_free(c);
635 }
636}
637
638static void
639client_extra_session2_setup(int id, void *arg)
640{
641 struct confirm_ctx *cctx = arg;
Damien Miller13390022005-07-06 09:44:19 +1000642 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +1000643 Channel *c;
Damien Miller3756dce2004-06-18 01:17:29 +1000644 int i;
Darren Tuckerfc959702004-07-17 16:12:08 +1000645
Damien Miller0e220db2004-06-15 10:34:08 +1000646 if (cctx == NULL)
647 fatal("%s: cctx == NULL", __func__);
648 if ((c = channel_lookup(id)) == NULL)
649 fatal("%s: no channel for id %d", __func__, id);
650
Damien Miller46d38de2005-07-17 17:02:09 +1000651 display = getenv("DISPLAY");
Damien Miller13390022005-07-06 09:44:19 +1000652 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
653 char *proto, *data;
654 /* Get reasonable local authentication information. */
655 client_x11_get_proto(display, options.xauth_location,
656 options.forward_x11_trusted, &proto, &data);
657 /* Request forwarding with authentication spoofing. */
658 debug("Requesting X11 forwarding with authentication spoofing.");
659 x11_request_forwarding_with_spoofing(id, display, proto, data);
660 /* XXX wait for reply */
661 }
662
663 if (cctx->want_agent_fwd && options.forward_agent) {
664 debug("Requesting authentication agent forwarding.");
665 channel_request_start(id, "auth-agent-req@openssh.com", 0);
666 packet_send();
667 }
668
Darren Tuckerfc959702004-07-17 16:12:08 +1000669 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
Damien Miller3756dce2004-06-18 01:17:29 +1000670 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
Damien Miller0e220db2004-06-15 10:34:08 +1000671 client_subsystem_reply);
Damien Miller23f07702004-06-18 01:19:03 +1000672
Damien Miller0e220db2004-06-15 10:34:08 +1000673 c->confirm_ctx = NULL;
674 buffer_free(&cctx->cmd);
Damien Miller3756dce2004-06-18 01:17:29 +1000675 xfree(cctx->term);
676 if (cctx->env != NULL) {
677 for (i = 0; cctx->env[i] != NULL; i++)
678 xfree(cctx->env[i]);
679 xfree(cctx->env);
Darren Tuckerfc959702004-07-17 16:12:08 +1000680 }
Damien Miller3756dce2004-06-18 01:17:29 +1000681 xfree(cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000682}
683
684static void
685client_process_control(fd_set * readset)
686{
687 Buffer m;
688 Channel *c;
Damien Millereccb9de2005-06-17 12:59:34 +1000689 int client_fd, new_fd[3], ver, allowed;
Damien Miller0e220db2004-06-15 10:34:08 +1000690 socklen_t addrlen;
691 struct sockaddr_storage addr;
692 struct confirm_ctx *cctx;
693 char *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +1000694 u_int i, len, env_len, command, flags;
Damien Miller0e220db2004-06-15 10:34:08 +1000695 uid_t euid;
696 gid_t egid;
697
698 /*
699 * Accept connection on control socket
700 */
701 if (control_fd == -1 || !FD_ISSET(control_fd, readset))
702 return;
703
704 memset(&addr, 0, sizeof(addr));
705 addrlen = sizeof(addr);
706 if ((client_fd = accept(control_fd,
707 (struct sockaddr*)&addr, &addrlen)) == -1) {
708 error("%s accept: %s", __func__, strerror(errno));
709 return;
710 }
711
712 if (getpeereid(client_fd, &euid, &egid) < 0) {
713 error("%s getpeereid failed: %s", __func__, strerror(errno));
714 close(client_fd);
715 return;
716 }
717 if ((euid != 0) && (getuid() != euid)) {
718 error("control mode uid mismatch: peer euid %u != uid %u",
719 (u_int) euid, (u_int) getuid());
720 close(client_fd);
721 return;
722 }
Damien Miller23f07702004-06-18 01:19:03 +1000723
Damien Miller0e220db2004-06-15 10:34:08 +1000724 unset_nonblock(client_fd);
725
Darren Tucker7ebfc102004-11-07 20:06:19 +1100726 /* Read command */
Damien Miller0e220db2004-06-15 10:34:08 +1000727 buffer_init(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100728 if (ssh_msg_recv(client_fd, &m) == -1) {
729 error("%s: client msg_recv failed", __func__);
730 close(client_fd);
731 buffer_free(&m);
732 return;
733 }
Damien Miller13390022005-07-06 09:44:19 +1000734 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100735 error("%s: wrong client version %d", __func__, ver);
736 buffer_free(&m);
737 close(client_fd);
738 return;
739 }
Damien Miller0e220db2004-06-15 10:34:08 +1000740
Darren Tucker7ebfc102004-11-07 20:06:19 +1100741 allowed = 1;
742 command = buffer_get_int(&m);
743 flags = buffer_get_int(&m);
744
745 buffer_clear(&m);
746
747 switch (command) {
748 case SSHMUX_COMMAND_OPEN:
Damien Millerd14b1e72005-06-16 13:19:41 +1000749 if (options.control_master == SSHCTL_MASTER_ASK ||
750 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100751 allowed = ask_permission("Allow shared connection "
752 "to %s? ", host);
753 /* continue below */
754 break;
755 case SSHMUX_COMMAND_TERMINATE:
Damien Millerd14b1e72005-06-16 13:19:41 +1000756 if (options.control_master == SSHCTL_MASTER_ASK ||
757 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100758 allowed = ask_permission("Terminate shared connection "
759 "to %s? ", host);
760 if (allowed)
761 quit_pending = 1;
Darren Tucker47eede72005-03-14 23:08:12 +1100762 /* FALLTHROUGH */
Darren Tucker7ebfc102004-11-07 20:06:19 +1100763 case SSHMUX_COMMAND_ALIVE_CHECK:
764 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
765 buffer_clear(&m);
766 buffer_put_int(&m, allowed);
767 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000768 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100769 error("%s: client msg_send failed", __func__);
770 close(client_fd);
771 buffer_free(&m);
772 return;
773 }
774 buffer_free(&m);
775 close(client_fd);
776 return;
777 default:
778 error("Unsupported command %d", command);
779 buffer_free(&m);
780 close(client_fd);
781 return;
782 }
783
784 /* Reply for SSHMUX_COMMAND_OPEN */
785 buffer_clear(&m);
Damien Miller23f07702004-06-18 01:19:03 +1000786 buffer_put_int(&m, allowed);
Damien Miller0e220db2004-06-15 10:34:08 +1000787 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000788 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000789 error("%s: client msg_send failed", __func__);
790 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000791 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000792 return;
793 }
Damien Miller0e220db2004-06-15 10:34:08 +1000794
Damien Miller23f07702004-06-18 01:19:03 +1000795 if (!allowed) {
796 error("Refused control connection");
797 close(client_fd);
798 buffer_free(&m);
799 return;
800 }
801
Darren Tucker7ebfc102004-11-07 20:06:19 +1100802 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000803 if (ssh_msg_recv(client_fd, &m) == -1) {
804 error("%s: client msg_recv failed", __func__);
805 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000806 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000807 return;
808 }
Damien Miller13390022005-07-06 09:44:19 +1000809 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Damien Miller0e220db2004-06-15 10:34:08 +1000810 error("%s: wrong client version %d", __func__, ver);
811 buffer_free(&m);
812 close(client_fd);
813 return;
814 }
815
816 cctx = xmalloc(sizeof(*cctx));
817 memset(cctx, 0, sizeof(*cctx));
Darren Tucker7ebfc102004-11-07 20:06:19 +1100818 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
819 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
Damien Miller13390022005-07-06 09:44:19 +1000820 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
821 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000822 cctx->term = buffer_get_string(&m, &len);
823
824 cmd = buffer_get_string(&m, &len);
825 buffer_init(&cctx->cmd);
826 buffer_append(&cctx->cmd, cmd, strlen(cmd));
827
Damien Miller3756dce2004-06-18 01:17:29 +1000828 env_len = buffer_get_int(&m);
829 env_len = MIN(env_len, 4096);
830 debug3("%s: receiving %d env vars", __func__, env_len);
831 if (env_len != 0) {
832 cctx->env = xmalloc(sizeof(*cctx->env) * (env_len + 1));
833 for (i = 0; i < env_len; i++)
834 cctx->env[i] = buffer_get_string(&m, &len);
835 cctx->env[i] = NULL;
836 }
837
Damien Miller0e220db2004-06-15 10:34:08 +1000838 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
839 cctx->want_tty, cctx->want_subsys, cmd);
840
841 /* Gather fds from client */
842 new_fd[0] = mm_receive_fd(client_fd);
843 new_fd[1] = mm_receive_fd(client_fd);
844 new_fd[2] = mm_receive_fd(client_fd);
845
846 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
847 new_fd[0], new_fd[1], new_fd[2]);
848
849 /* Try to pick up ttymodes from client before it goes raw */
850 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
851 error("%s: tcgetattr: %s", __func__, strerror(errno));
852
Darren Tucker7ebfc102004-11-07 20:06:19 +1100853 /* This roundtrip is just for synchronisation of ttymodes */
Damien Miller0e220db2004-06-15 10:34:08 +1000854 buffer_clear(&m);
Damien Miller13390022005-07-06 09:44:19 +1000855 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000856 error("%s: client msg_send failed", __func__);
857 close(client_fd);
858 close(new_fd[0]);
859 close(new_fd[1]);
860 close(new_fd[2]);
Damien Miller23f07702004-06-18 01:19:03 +1000861 buffer_free(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100862 xfree(cctx->term);
863 if (env_len != 0) {
864 for (i = 0; i < env_len; i++)
865 xfree(cctx->env[i]);
866 xfree(cctx->env);
867 }
Damien Miller0e220db2004-06-15 10:34:08 +1000868 return;
869 }
870 buffer_free(&m);
871
872 /* enable nonblocking unless tty */
873 if (!isatty(new_fd[0]))
874 set_nonblock(new_fd[0]);
875 if (!isatty(new_fd[1]))
876 set_nonblock(new_fd[1]);
877 if (!isatty(new_fd[2]))
878 set_nonblock(new_fd[2]);
879
880 set_nonblock(client_fd);
881
Darren Tuckerfc959702004-07-17 16:12:08 +1000882 c = channel_new("session", SSH_CHANNEL_OPENING,
Damien Miller0e220db2004-06-15 10:34:08 +1000883 new_fd[0], new_fd[1], new_fd[2],
884 CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT,
885 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
886
887 /* XXX */
888 c->ctl_fd = client_fd;
889
890 debug3("%s: channel_new: %d", __func__, c->self);
891
892 channel_send_open(c->self);
893 channel_register_confirm(c->self, client_extra_session2_setup, cctx);
894}
895
896static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000897process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000898{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000899 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100900 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000901 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000902 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100903 u_short cancel_port;
904 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000905
906 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000907 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000908 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000909 if (s == NULL)
910 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000911 while (*s && isspace(*s))
912 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000913 if (*s == '-')
914 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000915 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000916 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000917
Darren Tucker1973c882004-05-24 10:34:36 +1000918 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000919 logit("Commands:");
920 logit(" -Lport:host:hostport Request local forward");
921 logit(" -Rport:host:hostport Request remote forward");
922 logit(" -KRhostport Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100923 if (!options.permit_local_command)
924 goto out;
925 logit(" !args Execute local command");
926 goto out;
927 }
928
929 if (*s == '!' && options.permit_local_command) {
930 s++;
931 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000932 goto out;
933 }
934
935 if (*s == 'K') {
936 delete = 1;
937 s++;
938 }
939 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +1000940 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000941 goto out;
942 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000943 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000944 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +1000945 if (local && delete) {
946 logit("Not supported.");
947 goto out;
948 }
949 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000951 goto out;
952 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000953
954 s++;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000955 while (*s && isspace(*s))
956 s++;
957
Darren Tuckere7066df2004-05-24 10:18:05 +1000958 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100959 cancel_port = 0;
960 cancel_host = hpdelim(&s); /* may be NULL */
961 if (s != NULL) {
962 cancel_port = a2port(s);
963 cancel_host = cleanhostname(cancel_host);
964 } else {
965 cancel_port = a2port(cancel_host);
966 cancel_host = NULL;
967 }
968 if (cancel_port == 0) {
969 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000970 goto out;
971 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100972 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000973 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100974 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000975 logit("Bad forwarding specification.");
976 goto out;
977 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000978 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100979 if (channel_setup_local_fwd_listener(fwd.listen_host,
980 fwd.listen_port, fwd.connect_host,
981 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000982 logit("Port forwarding failed.");
983 goto out;
984 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100985 } else {
986 channel_request_remote_forwarding(fwd.listen_host,
987 fwd.listen_port, fwd.connect_host,
988 fwd.connect_port);
989 }
990
Darren Tuckere7066df2004-05-24 10:18:05 +1000991 logit("Forwarding port.");
992 }
993
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000994out:
995 signal(SIGINT, handler);
996 enter_raw_mode();
997 if (cmd)
998 xfree(cmd);
999}
1000
Damien Millerad833b32000-08-23 10:46:23 +10001001/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +00001002static int
Damien Millerad833b32000-08-23 10:46:23 +10001003process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1004{
1005 char string[1024];
1006 pid_t pid;
1007 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001008 u_int i;
1009 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001010 char *s;
1011
Damien Millereccb9de2005-06-17 12:59:34 +10001012 if (len <= 0)
1013 return (0);
1014
1015 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001016 /* Get one character at a time. */
1017 ch = buf[i];
1018
1019 if (escape_pending) {
1020 /* We have previously seen an escape character. */
1021 /* Clear the flag now. */
1022 escape_pending = 0;
1023
1024 /* Process the escaped character. */
1025 switch (ch) {
1026 case '.':
1027 /* Terminate the connection. */
1028 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1029 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001030
1031 quit_pending = 1;
1032 return -1;
1033
1034 case 'Z' - 64:
1035 /* Suspend the program. */
1036 /* Print a message to that effect to the user. */
1037 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1038 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001039
1040 /* Restore terminal modes and suspend. */
1041 client_suspend_self(bin, bout, berr);
1042
1043 /* We have been continued. */
1044 continue;
1045
Damien Miller54c45982003-05-15 10:20:13 +10001046 case 'B':
1047 if (compat20) {
1048 snprintf(string, sizeof string,
1049 "%cB\r\n", escape_char);
1050 buffer_append(berr, string,
1051 strlen(string));
1052 channel_request_start(session_ident,
1053 "break", 0);
1054 packet_put_int(1000);
1055 packet_send();
1056 }
1057 continue;
1058
Ben Lindstromf28f6342001-04-04 02:03:04 +00001059 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001060 if (compat20) {
1061 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +10001062 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001063 else
1064 need_rekeying = 1;
1065 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001066 continue;
1067
Damien Millerad833b32000-08-23 10:46:23 +10001068 case '&':
Damien Millerad833b32000-08-23 10:46:23 +10001069 /*
1070 * Detach the program (continue to serve connections,
1071 * but put in background and no more new connections).
1072 */
Damien Miller96507ef2001-11-12 10:52:25 +11001073 /* Restore tty modes. */
1074 leave_raw_mode();
1075
1076 /* Stop listening for new connections. */
1077 channel_stop_listening();
1078
1079 snprintf(string, sizeof string,
1080 "%c& [backgrounded]\n", escape_char);
1081 buffer_append(berr, string, strlen(string));
1082
1083 /* Fork into background. */
1084 pid = fork();
1085 if (pid < 0) {
1086 error("fork: %.100s", strerror(errno));
1087 continue;
1088 }
1089 if (pid != 0) { /* This is the parent. */
1090 /* The parent just exits. */
1091 exit(0);
1092 }
1093 /* The child continues serving connections. */
1094 if (compat20) {
1095 buffer_append(bin, "\004", 1);
1096 /* fake EOF on stdin */
1097 return -1;
1098 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001099 /*
1100 * Sending SSH_CMSG_EOF alone does not always appear
1101 * to be enough. So we try to send an EOF character
1102 * first.
1103 */
1104 packet_start(SSH_CMSG_STDIN_DATA);
1105 packet_put_string("\004", 1);
1106 packet_send();
1107 /* Close stdin. */
1108 stdin_eof = 1;
1109 if (buffer_len(bin) == 0) {
1110 packet_start(SSH_CMSG_EOF);
1111 packet_send();
1112 }
1113 }
Damien Miller96507ef2001-11-12 10:52:25 +11001114 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001115
1116 case '?':
1117 snprintf(string, sizeof string,
1118"%c?\r\n\
1119Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001120%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +10001121%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001122%cC - open a command line\r\n\
1123%cR - Request rekey (SSH protocol 2 only)\r\n\
1124%c^Z - suspend ssh\r\n\
1125%c# - list forwarded connections\r\n\
1126%c& - background ssh (when waiting for connections to terminate)\r\n\
1127%c? - this message\r\n\
1128%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001129(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +10001130 escape_char, escape_char, escape_char, escape_char,
1131 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +10001132 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001133 buffer_append(berr, string, strlen(string));
1134 continue;
1135
1136 case '#':
1137 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1138 buffer_append(berr, string, strlen(string));
1139 s = channel_open_message();
1140 buffer_append(berr, s, strlen(s));
1141 xfree(s);
1142 continue;
1143
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001144 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001145 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001146 continue;
1147
Damien Millerad833b32000-08-23 10:46:23 +10001148 default:
1149 if (ch != escape_char) {
1150 buffer_put_char(bin, escape_char);
1151 bytes++;
1152 }
1153 /* Escaped characters fall through here */
1154 break;
1155 }
1156 } else {
1157 /*
1158 * The previous character was not an escape char. Check if this
1159 * is an escape.
1160 */
1161 if (last_was_cr && ch == escape_char) {
1162 /* It is. Set the flag and continue to next character. */
1163 escape_pending = 1;
1164 continue;
1165 }
1166 }
1167
1168 /*
1169 * Normal character. Record whether it was a newline,
1170 * and append it to the buffer.
1171 */
1172 last_was_cr = (ch == '\r' || ch == '\n');
1173 buffer_put_char(bin, ch);
1174 bytes++;
1175 }
1176 return bytes;
1177}
1178
Ben Lindstrombba81212001-06-25 05:01:22 +00001179static void
Damien Miller1383bd82000-04-06 12:32:37 +10001180client_process_input(fd_set * readset)
1181{
Damien Miller166fca82000-04-20 07:42:21 +10001182 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001183 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001184
Damien Miller95def091999-11-25 00:26:21 +11001185 /* Read input from stdin. */
1186 if (FD_ISSET(fileno(stdin), readset)) {
1187 /* Read as much as possible. */
1188 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001189 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1190 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001191 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001192 /*
1193 * Received EOF or error. They are treated
1194 * similarly, except that an error message is printed
1195 * if it was an error condition.
1196 */
Damien Miller95def091999-11-25 00:26:21 +11001197 if (len < 0) {
1198 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1199 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001200 }
1201 /* Mark that we have seen EOF. */
1202 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001203 /*
1204 * Send an EOF message to the server unless there is
1205 * data in the buffer. If there is data in the
1206 * buffer, no message will be sent now. Code
1207 * elsewhere will send the EOF when the buffer
1208 * becomes empty if stdin_eof is set.
1209 */
Damien Miller95def091999-11-25 00:26:21 +11001210 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 packet_start(SSH_CMSG_EOF);
1212 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001213 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001214 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001215 /*
1216 * Normal successful read, and no escape character.
1217 * Just append the data to buffer.
1218 */
Damien Miller95def091999-11-25 00:26:21 +11001219 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001220 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001221 /*
1222 * Normal, successful read. But we have an escape character
1223 * and have to process the characters one by one.
1224 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +00001225 if (process_escapes(&stdin_buffer, &stdout_buffer,
1226 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001227 return;
Damien Miller95def091999-11-25 00:26:21 +11001228 }
1229 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230}
1231
Ben Lindstrombba81212001-06-25 05:01:22 +00001232static void
Damien Miller95def091999-11-25 00:26:21 +11001233client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234{
Damien Miller95def091999-11-25 00:26:21 +11001235 int len;
1236 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
Damien Miller95def091999-11-25 00:26:21 +11001238 /* Write buffered output to stdout. */
1239 if (FD_ISSET(fileno(stdout), writeset)) {
1240 /* Write as much data as possible. */
1241 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001242 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001243 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001244 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001245 len = 0;
1246 else {
Damien Miller5428f641999-11-25 11:54:57 +11001247 /*
1248 * An error or EOF was encountered. Put an
1249 * error message to stderr buffer.
1250 */
Damien Miller95def091999-11-25 00:26:21 +11001251 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1252 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001253 quit_pending = 1;
1254 return;
1255 }
1256 }
1257 /* Consume printed data from the buffer. */
1258 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001259 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260 }
Damien Miller95def091999-11-25 00:26:21 +11001261 /* Write buffered output to stderr. */
1262 if (FD_ISSET(fileno(stderr), writeset)) {
1263 /* Write as much data as possible. */
1264 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001265 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001266 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001267 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001268 len = 0;
1269 else {
Damien Miller5428f641999-11-25 11:54:57 +11001270 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001271 quit_pending = 1;
1272 return;
1273 }
1274 }
1275 /* Consume printed characters from the buffer. */
1276 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001277 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001278 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279}
1280
Damien Miller5428f641999-11-25 11:54:57 +11001281/*
Damien Millerb38eff82000-04-01 11:09:21 +10001282 * Get packets from the connection input buffer, and process them as long as
1283 * there are packets available.
1284 *
1285 * Any unknown packets received during the actual
1286 * session cause the session to terminate. This is
1287 * intended to make debugging easier since no
1288 * confirmations are sent. Any compatible protocol
1289 * extensions must be negotiated during the
1290 * preparatory phase.
1291 */
1292
Ben Lindstrombba81212001-06-25 05:01:22 +00001293static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001294client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001295{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001296 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001297}
1298
Damien Millerad833b32000-08-23 10:46:23 +10001299/* scan buf[] for '~' before sending data to the peer */
1300
Ben Lindstrombba81212001-06-25 05:01:22 +00001301static int
Damien Millerad833b32000-08-23 10:46:23 +10001302simple_escape_filter(Channel *c, char *buf, int len)
1303{
1304 /* XXX we assume c->extended is writeable */
1305 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1306}
1307
Ben Lindstrombba81212001-06-25 05:01:22 +00001308static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001309client_channel_closed(int id, void *arg)
1310{
Damien Miller3ec27592001-10-12 11:35:04 +10001311 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001312 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001313 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001314}
1315
Damien Millerb38eff82000-04-01 11:09:21 +10001316/*
Damien Miller5428f641999-11-25 11:54:57 +11001317 * Implements the interactive session with the server. This is called after
1318 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001319 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1320 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001321 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322
Damien Miller4af51302000-04-16 11:18:38 +10001323int
Damien Millerad833b32000-08-23 10:46:23 +10001324client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001325{
Damien Miller5e953212001-01-30 09:14:00 +11001326 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001327 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001328 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1329 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001330 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331
Damien Miller95def091999-11-25 00:26:21 +11001332 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Damien Miller95def091999-11-25 00:26:21 +11001334 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller95def091999-11-25 00:26:21 +11001336 /* Initialize variables. */
1337 escape_pending = 0;
1338 last_was_cr = 1;
1339 exit_status = -1;
1340 stdin_eof = 0;
1341 buffer_high = 64 * 1024;
1342 connection_in = packet_get_connection_in();
1343 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001344 max_fd = MAX(connection_in, connection_out);
Damien Miller0e220db2004-06-15 10:34:08 +10001345 if (control_fd != -1)
1346 max_fd = MAX(max_fd, control_fd);
Damien Miller5e953212001-01-30 09:14:00 +11001347
1348 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001349 /* enable nonblocking unless tty */
1350 if (!isatty(fileno(stdin)))
1351 set_nonblock(fileno(stdin));
1352 if (!isatty(fileno(stdout)))
1353 set_nonblock(fileno(stdout));
1354 if (!isatty(fileno(stderr)))
1355 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001356 max_fd = MAX(max_fd, fileno(stdin));
1357 max_fd = MAX(max_fd, fileno(stdout));
1358 max_fd = MAX(max_fd, fileno(stderr));
1359 }
Damien Miller95def091999-11-25 00:26:21 +11001360 stdin_bytes = 0;
1361 stdout_bytes = 0;
1362 stderr_bytes = 0;
1363 quit_pending = 0;
1364 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365
Damien Miller95def091999-11-25 00:26:21 +11001366 /* Initialize buffers. */
1367 buffer_init(&stdin_buffer);
1368 buffer_init(&stdout_buffer);
1369 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370
Damien Millerb38eff82000-04-01 11:09:21 +10001371 client_init_dispatch();
1372
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001373 /*
1374 * Set signal handlers, (e.g. to restore non-blocking mode)
1375 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1376 */
Darren Tucker07336da2004-11-05 20:02:16 +11001377 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1378 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001379 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1380 signal(SIGINT, signal_handler);
1381 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1382 signal(SIGQUIT, signal_handler);
1383 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1384 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001385 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386
Damien Miller95def091999-11-25 00:26:21 +11001387 if (have_pty)
1388 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Ben Lindstrom5b828322001-02-09 01:34:36 +00001390 if (compat20) {
1391 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001392 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001393 channel_register_filter(session_ident,
Damien Miller077b2382005-12-31 16:22:32 +11001394 simple_escape_filter, NULL);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001395 if (session_ident != -1)
1396 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001397 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001398 } else {
1399 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001400 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001401 }
Damien Millerad833b32000-08-23 10:46:23 +10001402
Damien Miller95def091999-11-25 00:26:21 +11001403 /* Main loop of the client for the interactive session mode. */
1404 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
Damien Miller5428f641999-11-25 11:54:57 +11001406 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001407 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001408
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001409 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001410 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001411
1412 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001413
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001414 if (rekeying) {
1415 debug("rekeying in progress");
1416 } else {
1417 /*
1418 * Make packets of buffered stdin data, and buffer
1419 * them for sending to the server.
1420 */
1421 if (!compat20)
1422 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001424 /*
1425 * Make packets from buffered channel data, and
1426 * enqueue them for sending to the server.
1427 */
1428 if (packet_not_very_much_data_to_write())
1429 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001431 /*
1432 * Check if the window size has changed, and buffer a
1433 * message about it to the server if so.
1434 */
1435 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001437 if (quit_pending)
1438 break;
1439 }
Damien Miller5428f641999-11-25 11:54:57 +11001440 /*
1441 * Wait until we have something to do (something becomes
1442 * available on one of the descriptors).
1443 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001444 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001445 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001446 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller95def091999-11-25 00:26:21 +11001448 if (quit_pending)
1449 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001451 /* Do channel operations unless rekeying in progress. */
1452 if (!rekeying) {
1453 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001454 if (need_rekeying || packet_need_rekeying()) {
1455 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001456 xxx_kex->done = 0;
1457 kex_send_kexinit(xxx_kex);
1458 need_rekeying = 0;
1459 }
1460 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
Damien Miller1383bd82000-04-06 12:32:37 +10001462 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001463 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464
Damien Miller0e220db2004-06-15 10:34:08 +10001465 /* Accept control connections. */
1466 client_process_control(readset);
1467
Damien Miller1383bd82000-04-06 12:32:37 +10001468 if (quit_pending)
1469 break;
1470
1471 if (!compat20) {
1472 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001473 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001474 /*
1475 * Process output to stdout and stderr. Output to
1476 * the connection is processed elsewhere (above).
1477 */
Damien Miller5e953212001-01-30 09:14:00 +11001478 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001479 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller5428f641999-11-25 11:54:57 +11001481 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001482 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001483 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484 }
Damien Miller5e953212001-01-30 09:14:00 +11001485 if (readset)
1486 xfree(readset);
1487 if (writeset)
1488 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller95def091999-11-25 00:26:21 +11001490 /* Terminate the session. */
1491
1492 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001493 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001494
Ben Lindstrom601e4362001-06-21 03:19:23 +00001495 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001496
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001497 if (have_pty)
1498 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001499
1500 /* restore blocking io */
1501 if (!isatty(fileno(stdin)))
1502 unset_nonblock(fileno(stdin));
1503 if (!isatty(fileno(stdout)))
1504 unset_nonblock(fileno(stdout));
1505 if (!isatty(fileno(stderr)))
1506 unset_nonblock(fileno(stderr));
1507
Damien Millerd6965512003-12-17 16:31:53 +11001508 /*
1509 * If there was no shell or command requested, there will be no remote
1510 * exit status to be returned. In that case, clear error code if the
1511 * connection was deliberately terminated at this end.
1512 */
1513 if (no_shell_flag && received_signal == SIGTERM) {
1514 received_signal = 0;
1515 exit_status = 0;
1516 }
1517
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001518 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001519 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001520
1521 /*
1522 * In interactive mode (with pseudo tty) display a message indicating
1523 * that the connection has been closed.
1524 */
1525 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1526 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1527 buffer_append(&stderr_buffer, buf, strlen(buf));
1528 }
1529
Damien Miller95def091999-11-25 00:26:21 +11001530 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001531 while (buffer_len(&stdout_buffer) > 0) {
1532 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001533 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001534 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001535 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001536 break;
1537 }
Damien Miller95def091999-11-25 00:26:21 +11001538 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001539 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001540 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541
Damien Miller95def091999-11-25 00:26:21 +11001542 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001543 while (buffer_len(&stderr_buffer) > 0) {
1544 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001545 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001546 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001547 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001548 break;
1549 }
Damien Miller95def091999-11-25 00:26:21 +11001550 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001551 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001552 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Damien Miller95def091999-11-25 00:26:21 +11001554 /* Clear and free any buffers. */
1555 memset(buf, 0, sizeof(buf));
1556 buffer_free(&stdin_buffer);
1557 buffer_free(&stdout_buffer);
1558 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001559
Damien Miller95def091999-11-25 00:26:21 +11001560 /* Report bytes transferred, and transfer rates. */
1561 total_time = get_current_time() - start_time;
1562 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001563 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001564 if (total_time > 0)
1565 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001566 stdin_bytes / total_time, stdout_bytes / total_time,
1567 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001568
1569 /* Return the exit status of the program. */
1570 debug("Exit status %d", exit_status);
1571 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572}
Damien Millerb38eff82000-04-01 11:09:21 +10001573
1574/*********/
1575
Ben Lindstrombba81212001-06-25 05:01:22 +00001576static void
Damien Miller630d6f42002-01-22 23:17:30 +11001577client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001578{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001579 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001580 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001581 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001582 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001583 memset(data, 0, data_len);
1584 xfree(data);
1585}
Ben Lindstrombba81212001-06-25 05:01:22 +00001586static void
Damien Miller630d6f42002-01-22 23:17:30 +11001587client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001588{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001589 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001590 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001591 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001592 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001593 memset(data, 0, data_len);
1594 xfree(data);
1595}
Ben Lindstrombba81212001-06-25 05:01:22 +00001596static void
Damien Miller630d6f42002-01-22 23:17:30 +11001597client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001598{
Damien Millerb38eff82000-04-01 11:09:21 +10001599 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001600 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001601 /* Acknowledge the exit. */
1602 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1603 packet_send();
1604 /*
1605 * Must wait for packet to be sent since we are
1606 * exiting the loop.
1607 */
1608 packet_write_wait();
1609 /* Flag that we want to exit. */
1610 quit_pending = 1;
1611}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001612static void
1613client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1614{
1615 Channel *c = NULL;
1616 int remote_id, sock;
1617
1618 /* Read the remote channel number from the message. */
1619 remote_id = packet_get_int();
1620 packet_check_eom();
1621
1622 /*
1623 * Get a connection to the local authentication agent (this may again
1624 * get forwarded).
1625 */
1626 sock = ssh_get_authentication_socket();
1627
1628 /*
1629 * If we could not connect the agent, send an error message back to
1630 * the server. This should never happen unless the agent dies,
1631 * because authentication forwarding is only enabled if we have an
1632 * agent.
1633 */
1634 if (sock >= 0) {
1635 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1636 -1, 0, 0, 0, "authentication agent connection", 1);
1637 c->remote_id = remote_id;
1638 c->force_drain = 1;
1639 }
1640 if (c == NULL) {
1641 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1642 packet_put_int(remote_id);
1643 } else {
1644 /* Send a confirmation to the remote host. */
1645 debug("Forwarding authentication connection.");
1646 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1647 packet_put_int(remote_id);
1648 packet_put_int(c->self);
1649 }
1650 packet_send();
1651}
Damien Millerb38eff82000-04-01 11:09:21 +10001652
Ben Lindstrombba81212001-06-25 05:01:22 +00001653static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001654client_request_forwarded_tcpip(const char *request_type, int rchan)
1655{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001656 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001657 char *listen_address, *originator_address;
1658 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001659 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001660
1661 /* Get rest of the packet */
1662 listen_address = packet_get_string(NULL);
1663 listen_port = packet_get_int();
1664 originator_address = packet_get_string(NULL);
1665 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001666 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001667
1668 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1669 listen_address, listen_port, originator_address, originator_port);
1670
Ben Lindstrom173e6462001-07-04 05:15:15 +00001671 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001672 if (sock < 0) {
1673 xfree(originator_address);
1674 xfree(listen_address);
1675 return NULL;
1676 }
1677 c = channel_new("forwarded-tcpip",
1678 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1679 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001680 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001681 xfree(originator_address);
1682 xfree(listen_address);
1683 return c;
1684}
1685
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001686static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001687client_request_x11(const char *request_type, int rchan)
1688{
1689 Channel *c = NULL;
1690 char *originator;
1691 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001692 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001693
1694 if (!options.forward_x11) {
1695 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001696 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001697 return NULL;
1698 }
1699 originator = packet_get_string(NULL);
1700 if (datafellows & SSH_BUG_X11FWD) {
1701 debug2("buggy server: x11 request w/o originator_port");
1702 originator_port = 0;
1703 } else {
1704 originator_port = packet_get_int();
1705 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001706 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001708 debug("client_request_x11: request from %s %d", originator,
1709 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001710 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001711 sock = x11_connect_display();
1712 if (sock < 0)
1713 return NULL;
1714 c = channel_new("x11",
1715 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001716 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001717 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718 return c;
1719}
1720
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001721static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001722client_request_agent(const char *request_type, int rchan)
1723{
1724 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001725 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001726
1727 if (!options.forward_agent) {
1728 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001729 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001730 return NULL;
1731 }
1732 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001733 if (sock < 0)
1734 return NULL;
1735 c = channel_new("authentication agent connection",
1736 SSH_CHANNEL_OPEN, sock, sock, -1,
1737 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001738 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001739 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740 return c;
1741}
1742
Damien Millerbd483e72000-04-30 10:00:53 +10001743/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001744static void
Damien Miller630d6f42002-01-22 23:17:30 +11001745client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001746{
1747 Channel *c = NULL;
1748 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001749 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001750 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001751
1752 ctype = packet_get_string(&len);
1753 rchan = packet_get_int();
1754 rwindow = packet_get_int();
1755 rmaxpack = packet_get_int();
1756
Damien Millere247cc42000-05-07 12:03:14 +10001757 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001758 ctype, rchan, rwindow, rmaxpack);
1759
Damien Miller0bc1bd82000-11-13 22:57:25 +11001760 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1761 c = client_request_forwarded_tcpip(ctype, rchan);
1762 } else if (strcmp(ctype, "x11") == 0) {
1763 c = client_request_x11(ctype, rchan);
1764 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1765 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001766 }
1767/* XXX duplicate : */
1768 if (c != NULL) {
1769 debug("confirm %s", ctype);
1770 c->remote_id = rchan;
1771 c->remote_window = rwindow;
1772 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001773 if (c->type != SSH_CHANNEL_CONNECTING) {
1774 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1775 packet_put_int(c->remote_id);
1776 packet_put_int(c->self);
1777 packet_put_int(c->local_window);
1778 packet_put_int(c->local_maxpacket);
1779 packet_send();
1780 }
Damien Millerbd483e72000-04-30 10:00:53 +10001781 } else {
1782 debug("failure %s", ctype);
1783 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1784 packet_put_int(rchan);
1785 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001786 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001787 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001788 packet_put_cstring("");
1789 }
Damien Millerbd483e72000-04-30 10:00:53 +10001790 packet_send();
1791 }
1792 xfree(ctype);
1793}
Ben Lindstrombba81212001-06-25 05:01:22 +00001794static void
Damien Miller630d6f42002-01-22 23:17:30 +11001795client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001796{
1797 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001798 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001799 char *rtype;
1800
1801 id = packet_get_int();
1802 rtype = packet_get_string(NULL);
1803 reply = packet_get_char();
1804
1805 debug("client_input_channel_req: channel %d rtype %s reply %d",
1806 id, rtype, reply);
1807
Damien Miller3bbd8782004-06-18 22:23:22 +10001808 if (id == -1) {
1809 error("client_input_channel_req: request for channel -1");
1810 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001811 error("client_input_channel_req: channel %d: unknown channel", id);
1812 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001813 exitval = packet_get_int();
1814 if (id == session_ident) {
1815 success = 1;
1816 exit_status = exitval;
1817 } else if (c->ctl_fd == -1) {
1818 error("client_input_channel_req: unexpected channel %d",
1819 session_ident);
1820 } else {
1821 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1822 success = 1;
1823 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001824 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001825 }
1826 if (reply) {
1827 packet_start(success ?
1828 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001829 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001830 packet_send();
1831 }
1832 xfree(rtype);
1833}
Damien Millerc3fa4072002-01-22 23:21:58 +11001834static void
1835client_input_global_request(int type, u_int32_t seq, void *ctxt)
1836{
1837 char *rtype;
1838 int want_reply;
1839 int success = 0;
1840
1841 rtype = packet_get_string(NULL);
1842 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001843 debug("client_input_global_request: rtype %s want_reply %d",
1844 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001845 if (want_reply) {
1846 packet_start(success ?
1847 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1848 packet_send();
1849 packet_write_wait();
1850 }
1851 xfree(rtype);
1852}
Damien Millerbd483e72000-04-30 10:00:53 +10001853
Damien Miller0e220db2004-06-15 10:34:08 +10001854void
Darren Tuckerfc959702004-07-17 16:12:08 +10001855client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller3756dce2004-06-18 01:17:29 +10001856 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
Damien Miller0e220db2004-06-15 10:34:08 +10001857 dispatch_fn *subsys_repl)
1858{
1859 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001860 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001861
1862 debug2("%s: id %d", __func__, id);
1863
Darren Tucker5d78de62004-11-05 20:35:44 +11001864 if ((c = channel_lookup(id)) == NULL)
1865 fatal("client_session2_setup: channel %d: unknown channel", id);
1866
Damien Miller0e220db2004-06-15 10:34:08 +10001867 if (want_tty) {
1868 struct winsize ws;
1869 struct termios tio;
1870
1871 /* Store window size in the packet. */
1872 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1873 memset(&ws, 0, sizeof(ws));
1874
1875 channel_request_start(id, "pty-req", 0);
1876 packet_put_cstring(term != NULL ? term : "");
1877 packet_put_int(ws.ws_col);
1878 packet_put_int(ws.ws_row);
1879 packet_put_int(ws.ws_xpixel);
1880 packet_put_int(ws.ws_ypixel);
1881 tio = get_saved_tio();
1882 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
1883 packet_send();
1884 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001885 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001886 }
1887
1888 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001889 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001890 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001891 char *name, *val;
1892
1893 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001894 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001895 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001896 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001897 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001898 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001899 continue;
1900 }
1901 *val++ = '\0';
1902
1903 matched = 0;
1904 for (j = 0; j < options.num_send_env; j++) {
1905 if (match_pattern(name, options.send_env[j])) {
1906 matched = 1;
1907 break;
1908 }
1909 }
1910 if (!matched) {
1911 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001912 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001913 continue;
1914 }
1915
1916 debug("Sending env %s = %s", name, val);
1917 channel_request_start(id, "env", 0);
1918 packet_put_cstring(name);
1919 packet_put_cstring(val);
1920 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001921 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001922 }
1923 }
1924
1925 len = buffer_len(cmd);
1926 if (len > 0) {
1927 if (len > 900)
1928 len = 900;
1929 if (want_subsystem) {
1930 debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
1931 channel_request_start(id, "subsystem", subsys_repl != NULL);
1932 if (subsys_repl != NULL) {
1933 /* register callback for reply */
1934 /* XXX we assume that client_loop has already been called */
1935 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
1936 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
1937 }
1938 } else {
1939 debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
1940 channel_request_start(id, "exec", 0);
1941 }
1942 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1943 packet_send();
1944 } else {
1945 channel_request_start(id, "shell", 0);
1946 packet_send();
1947 }
1948}
1949
Ben Lindstrombba81212001-06-25 05:01:22 +00001950static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001951client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001952{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001953 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001954
Damien Miller1383bd82000-04-06 12:32:37 +10001955 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1956 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1957 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1958 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001959 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001960 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1961 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001962 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001963 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001964 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001965
1966 /* rekeying */
1967 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001968
1969 /* global request reply messages */
1970 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1971 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001972}
Ben Lindstrombba81212001-06-25 05:01:22 +00001973static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001974client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001975{
1976 dispatch_init(NULL);
1977 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1978 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1979 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001980 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1981 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1982 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001983 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1984 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1985 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001986
1987 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10001988 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11001989 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1990 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001991}
Ben Lindstrombba81212001-06-25 05:01:22 +00001992static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001993client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001994{
1995 client_init_dispatch_13();
1996 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1997 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1998}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001999static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002000client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002001{
Damien Miller1383bd82000-04-06 12:32:37 +10002002 if (compat20)
2003 client_init_dispatch_20();
2004 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002005 client_init_dispatch_13();
2006 else
2007 client_init_dispatch_15();
2008}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002009
2010/* client specific fatal cleanup */
2011void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002012cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002013{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002014 leave_raw_mode();
2015 leave_non_blocking();
Damien Miller0e220db2004-06-15 10:34:08 +10002016 if (options.control_path != NULL && control_fd != -1)
2017 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002018 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002019}