blob: 9cbc1b0ce473656d2a1a72e0ac2df42b07e29806 [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"
Darren Tuckere1675822004-05-24 10:13:07 +100062RCSID("$OpenBSD: clientloop.c,v 1.120 2004/05/20 10:58:05 dtucker Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "ssh1.h"
66#include "ssh2.h"
67#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#include "packet.h"
69#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100070#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000071#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100072#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100073#include "buffer.h"
74#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110075#include "key.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +000076#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000079#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "authfd.h"
81#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100082#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +000083#include "misc.h"
Damien Miller69b69aa2000-10-28 14:19:58 +110084
85/* import options */
86extern Options options;
87
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088/* Flag indicating that stdin should be redirected from /dev/null. */
89extern int stdin_null_flag;
90
Damien Millerd6965512003-12-17 16:31:53 +110091/* Flag indicating that no shell has been requested */
92extern int no_shell_flag;
93
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Name of the host we are connecting to. This is the name given on the
96 * command line, or the HostName specified for the user-supplied name in a
97 * configuration file.
98 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099extern char *host;
100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Flag to indicate that we have received a window change signal which has
103 * not yet been processed. This will cause a message indicating the new
104 * window size to be sent to the server a little later. This is volatile
105 * because this is updated in a signal handler.
106 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000107static volatile sig_atomic_t received_window_change_signal = 0;
108static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
111static int in_non_blocking_mode = 0;
112
113/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000114static int quit_pending; /* Set to non-zero to quit the client loop. */
115static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100116static int escape_pending; /* Last character was the escape character */
117static int last_was_cr; /* Last character was a newline. */
118static int exit_status; /* Used to store the exit status of the command. */
119static int stdin_eof; /* EOF has been encountered on standard error. */
120static Buffer stdin_buffer; /* Buffer for stdin data. */
121static Buffer stdout_buffer; /* Buffer for stdout data. */
122static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000123static u_long stdin_bytes, stdout_bytes, stderr_bytes;
124static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100125static int connection_in; /* Connection to server (input). */
126static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000127static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000128static int session_closed = 0; /* In SSH2: login session closed. */
Damien Miller509b0102003-12-17 16:33:10 +1100129static int server_alive_timeouts = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000132int session_ident = -1;
133
Ben Lindstromf28f6342001-04-04 02:03:04 +0000134/*XXX*/
135extern Kex *xxx_kex;
136
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137/* Restores stdin to blocking mode. */
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000140leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller95def091999-11-25 00:26:21 +1100142 if (in_non_blocking_mode) {
143 (void) fcntl(fileno(stdin), F_SETFL, 0);
144 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100145 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146}
147
Damien Miller95def091999-11-25 00:26:21 +1100148/* Puts stdin terminal in non-blocking mode. */
149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000151enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 in_non_blocking_mode = 1;
154 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155}
156
Damien Miller5428f641999-11-25 11:54:57 +1100157/*
158 * Signal handler for the window change signal (SIGWINCH). This just sets a
159 * flag indicating that the window has changed.
160 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Miller95def091999-11-25 00:26:21 +1100163window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 received_window_change_signal = 1;
166 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}
168
Damien Miller5428f641999-11-25 11:54:57 +1100169/*
170 * Signal handler for signals that cause the program to terminate. These
171 * signals must be trapped to restore terminal modes.
172 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static void
Damien Miller95def091999-11-25 00:26:21 +1100175signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000177 received_signal = sig;
178 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Damien Miller5428f641999-11-25 11:54:57 +1100181/*
182 * Returns current time in seconds from Jan 1, 1970 with the maximum
183 * available resolution.
184 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000187get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188{
Damien Miller95def091999-11-25 00:26:21 +1100189 struct timeval tv;
190 gettimeofday(&tv, NULL);
191 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Miller5428f641999-11-25 11:54:57 +1100194/*
195 * This is called when the interactive is entered. This checks if there is
196 * an EOF coming on stdin. We must check this explicitly, as select() does
197 * not appear to wake up when redirecting from /dev/null.
198 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000201client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller95def091999-11-25 00:26:21 +1100203 int len;
204 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller5428f641999-11-25 11:54:57 +1100206 /*
207 * If standard input is to be "redirected from /dev/null", we simply
208 * mark that we have seen an EOF and send an EOF message to the
209 * server. Otherwise, we try to read a single character; it appears
210 * that for some files, such /dev/null, select() never wakes up for
211 * read for this descriptor, which means that we never get EOF. This
212 * way we will get the EOF if stdin comes from /dev/null or similar.
213 */
Damien Miller95def091999-11-25 00:26:21 +1100214 if (stdin_null_flag) {
215 /* Fake EOF on stdin. */
216 debug("Sending eof.");
217 stdin_eof = 1;
218 packet_start(SSH_CMSG_EOF);
219 packet_send();
220 } else {
Damien Miller95def091999-11-25 00:26:21 +1100221 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Damien Miller95def091999-11-25 00:26:21 +1100223 /* Check for immediate EOF on stdin. */
224 len = read(fileno(stdin), buf, 1);
225 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100226 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100227 debug("Sending eof.");
228 stdin_eof = 1;
229 packet_start(SSH_CMSG_EOF);
230 packet_send();
231 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100232 /*
233 * Got data. We must store the data in the buffer,
234 * and also process it as an escape character if
235 * appropriate.
236 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000237 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100238 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000239 else
Damien Miller95def091999-11-25 00:26:21 +1100240 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100241 }
Damien Miller95def091999-11-25 00:26:21 +1100242 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller5428f641999-11-25 11:54:57 +1100247/*
248 * Make packets from buffered stdin data, and buffer them for sending to the
249 * connection.
250 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000253client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000255 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Send buffered stdin data to the server. */
258 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100259 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100260 len = buffer_len(&stdin_buffer);
261 /* Keep the packets at reasonable size. */
262 if (len > packet_get_maxsize())
263 len = packet_get_maxsize();
264 packet_start(SSH_CMSG_STDIN_DATA);
265 packet_put_string(buffer_ptr(&stdin_buffer), len);
266 packet_send();
267 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000268 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100269 /* If we have a pending EOF, send it now. */
270 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
271 packet_start(SSH_CMSG_EOF);
272 packet_send();
273 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Damien Miller5428f641999-11-25 11:54:57 +1100277/*
278 * Checks if the client window has changed, and sends a packet about it to
279 * the server if so. The actual change is detected elsewhere (by a software
280 * interrupt on Unix); this just checks the flag and sends a message if
281 * appropriate.
282 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000285client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Damien Miller1383bd82000-04-06 12:32:37 +1000287 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288
Damien Miller1383bd82000-04-06 12:32:37 +1000289 if (! received_window_change_signal)
290 return;
291 /** XXX race */
292 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Miller1383bd82000-04-06 12:32:37 +1000294 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
295 return;
296
Damien Millerd3444942000-09-30 14:20:03 +1100297 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000298
299 if (compat20) {
300 channel_request_start(session_ident, "window-change", 0);
301 packet_put_int(ws.ws_col);
302 packet_put_int(ws.ws_row);
303 packet_put_int(ws.ws_xpixel);
304 packet_put_int(ws.ws_ypixel);
305 packet_send();
306 } else {
307 packet_start(SSH_CMSG_WINDOW_SIZE);
308 packet_put_int(ws.ws_row);
309 packet_put_int(ws.ws_col);
310 packet_put_int(ws.ws_xpixel);
311 packet_put_int(ws.ws_ypixel);
312 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314}
315
Damien Miller509b0102003-12-17 16:33:10 +1100316static void
317client_global_request_reply(int type, u_int32_t seq, void *ctxt)
318{
319 server_alive_timeouts = 0;
320 client_global_request_reply_fwd(type, seq, ctxt);
321}
322
323static void
324server_alive_check(void)
325{
326 if (++server_alive_timeouts > options.server_alive_count_max)
327 packet_disconnect("Timeout, server not responding.");
328 packet_start(SSH2_MSG_GLOBAL_REQUEST);
329 packet_put_cstring("keepalive@openssh.com");
330 packet_put_char(1); /* boolean: want reply */
331 packet_send();
332}
333
Damien Miller5428f641999-11-25 11:54:57 +1100334/*
335 * Waits until the client can do something (some data becomes available on
336 * one of the file descriptors).
337 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
Ben Lindstrombba81212001-06-25 05:01:22 +0000339static void
Damien Miller5e953212001-01-30 09:14:00 +1100340client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000341 int *maxfdp, int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Damien Miller509b0102003-12-17 16:33:10 +1100343 struct timeval tv, *tvp;
344 int ret;
345
Damien Miller5e953212001-01-30 09:14:00 +1100346 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000347 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348
Damien Miller1383bd82000-04-06 12:32:37 +1000349 if (!compat20) {
350 /* Read from the connection, unless our buffers are full. */
351 if (buffer_len(&stdout_buffer) < buffer_high &&
352 buffer_len(&stderr_buffer) < buffer_high &&
353 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100354 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000355 /*
356 * Read from stdin, unless we have seen EOF or have very much
357 * buffered data to send to the server.
358 */
359 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100360 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000361
362 /* Select stdout/stderr if have data in buffer. */
363 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100364 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000365 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100366 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000367 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000368 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000369 if (session_closed && !channel_still_open() &&
370 !packet_have_data_to_write()) {
371 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100372 memset(*readsetp, 0, *nallocp);
373 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000374 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000375 } else {
376 FD_SET(connection_in, *readsetp);
377 }
Damien Miller1383bd82000-04-06 12:32:37 +1000378 }
379
Damien Miller95def091999-11-25 00:26:21 +1100380 /* Select server connection if have data to write to the server. */
381 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100382 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100383
Damien Miller5428f641999-11-25 11:54:57 +1100384 /*
385 * Wait for something to happen. This will suspend the process until
386 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100387 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100388 */
Damien Miller95def091999-11-25 00:26:21 +1100389
Damien Miller509b0102003-12-17 16:33:10 +1100390 if (options.server_alive_interval == 0 || !compat20)
391 tvp = NULL;
392 else {
393 tv.tv_sec = options.server_alive_interval;
394 tv.tv_usec = 0;
395 tvp = &tv;
396 }
397 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
398 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100399 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000400
401 /*
402 * We have to clear the select masks, because we return.
403 * We have to return, because the mainloop checks for the flags
404 * set by the signal handlers.
405 */
Damien Miller79faeff2001-11-12 11:06:32 +1100406 memset(*readsetp, 0, *nallocp);
407 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000408
Damien Miller95def091999-11-25 00:26:21 +1100409 if (errno == EINTR)
410 return;
411 /* Note: we might still have data in the buffers. */
412 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
413 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100414 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100415 } else if (ret == 0)
416 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
Ben Lindstrombba81212001-06-25 05:01:22 +0000419static void
Damien Millerad833b32000-08-23 10:46:23 +1000420client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421{
Damien Miller95def091999-11-25 00:26:21 +1100422 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000425 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000426 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000427 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000428 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
Damien Miller95def091999-11-25 00:26:21 +1100430 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller5428f641999-11-25 11:54:57 +1100432 /*
433 * Free (and clear) the buffer to reduce the amount of data that gets
434 * written to swap.
435 */
Damien Millerad833b32000-08-23 10:46:23 +1000436 buffer_free(bin);
437 buffer_free(bout);
438 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 /* Save old window size. */
441 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Damien Miller95def091999-11-25 00:26:21 +1100443 /* Send the suspend signal to the program itself. */
444 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Damien Miller95def091999-11-25 00:26:21 +1100446 /* Check if the window size has changed. */
447 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
448 (oldws.ws_row != newws.ws_row ||
Damien Miller9f0f5c62001-12-21 14:45:46 +1100449 oldws.ws_col != newws.ws_col ||
450 oldws.ws_xpixel != newws.ws_xpixel ||
451 oldws.ws_ypixel != newws.ws_ypixel))
Damien Miller95def091999-11-25 00:26:21 +1100452 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453
Damien Miller95def091999-11-25 00:26:21 +1100454 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000455 buffer_init(bin);
456 buffer_init(bout);
457 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Damien Miller95def091999-11-25 00:26:21 +1100459 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460}
461
Ben Lindstrombba81212001-06-25 05:01:22 +0000462static void
Damien Miller1383bd82000-04-06 12:32:37 +1000463client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464{
Damien Miller1383bd82000-04-06 12:32:37 +1000465 int len;
466 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller5428f641999-11-25 11:54:57 +1100468 /*
469 * Read input from the server, and add any such data to the buffer of
470 * the packet subsystem.
471 */
Damien Miller95def091999-11-25 00:26:21 +1100472 if (FD_ISSET(connection_in, readset)) {
473 /* Read as much as possible. */
474 len = read(connection_in, buf, sizeof(buf));
475 if (len == 0) {
476 /* Received EOF. The remote host has closed the connection. */
477 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
478 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480 quit_pending = 1;
481 return;
Damien Miller95def091999-11-25 00:26:21 +1100482 }
Damien Miller5428f641999-11-25 11:54:57 +1100483 /*
484 * There is a kernel bug on Solaris that causes select to
485 * sometimes wake up even though there is no data available.
486 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000487 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100488 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller95def091999-11-25 00:26:21 +1100490 if (len < 0) {
491 /* An error has encountered. Perhaps there is a network problem. */
492 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
493 host, strerror(errno));
494 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100495 quit_pending = 1;
496 return;
497 }
498 packet_process_incoming(buf, len);
499 }
Damien Miller1383bd82000-04-06 12:32:37 +1000500}
501
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000502static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000503process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000504{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000505 void (*handler)(int);
506 char *s, *cmd;
507 u_short fwd_port, fwd_host_port;
508 char buf[1024], sfwd_port[6], sfwd_host_port[6];
509 int local = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000510
511 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000512 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000513 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000514 if (s == NULL)
515 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000516 while (*s && isspace(*s))
517 s++;
Darren Tuckere1675822004-05-24 10:13:07 +1000518 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000519 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000520 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
Damien Miller996acd22003-04-09 20:59:48 +1000521 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000522 goto out;
523 }
524 if (s[1] == 'L')
525 local = 1;
526 if (!local && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000527 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000528 goto out;
529 }
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000530 s += 2;
531 while (*s && isspace(*s))
532 s++;
533
534 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
535 sfwd_port, buf, sfwd_host_port) != 3 &&
536 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]",
537 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Miller996acd22003-04-09 20:59:48 +1000538 logit("Bad forwarding specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000539 goto out;
540 }
541 if ((fwd_port = a2port(sfwd_port)) == 0 ||
542 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000543 logit("Bad forwarding port(s).");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000544 goto out;
545 }
546 if (local) {
Ben Lindstrom681d9322002-03-22 03:53:00 +0000547 if (channel_setup_local_fwd_listener(fwd_port, buf,
548 fwd_host_port, options.gateway_ports) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000549 logit("Port forwarding failed.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000550 goto out;
551 }
552 } else
553 channel_request_remote_forwarding(fwd_port, buf,
554 fwd_host_port);
Damien Miller996acd22003-04-09 20:59:48 +1000555 logit("Forwarding port.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000556out:
557 signal(SIGINT, handler);
558 enter_raw_mode();
559 if (cmd)
560 xfree(cmd);
561}
562
Damien Millerad833b32000-08-23 10:46:23 +1000563/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +0000564static int
Damien Millerad833b32000-08-23 10:46:23 +1000565process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
566{
567 char string[1024];
568 pid_t pid;
569 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000570 u_int i;
571 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000572 char *s;
573
574 for (i = 0; i < len; i++) {
575 /* Get one character at a time. */
576 ch = buf[i];
577
578 if (escape_pending) {
579 /* We have previously seen an escape character. */
580 /* Clear the flag now. */
581 escape_pending = 0;
582
583 /* Process the escaped character. */
584 switch (ch) {
585 case '.':
586 /* Terminate the connection. */
587 snprintf(string, sizeof string, "%c.\r\n", escape_char);
588 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000589
590 quit_pending = 1;
591 return -1;
592
593 case 'Z' - 64:
594 /* Suspend the program. */
595 /* Print a message to that effect to the user. */
596 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
597 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000598
599 /* Restore terminal modes and suspend. */
600 client_suspend_self(bin, bout, berr);
601
602 /* We have been continued. */
603 continue;
604
Damien Miller54c45982003-05-15 10:20:13 +1000605 case 'B':
606 if (compat20) {
607 snprintf(string, sizeof string,
608 "%cB\r\n", escape_char);
609 buffer_append(berr, string,
610 strlen(string));
611 channel_request_start(session_ident,
612 "break", 0);
613 packet_put_int(1000);
614 packet_send();
615 }
616 continue;
617
Ben Lindstromf28f6342001-04-04 02:03:04 +0000618 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000619 if (compat20) {
620 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +1000621 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000622 else
623 need_rekeying = 1;
624 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000625 continue;
626
Damien Millerad833b32000-08-23 10:46:23 +1000627 case '&':
Damien Millerad833b32000-08-23 10:46:23 +1000628 /*
629 * Detach the program (continue to serve connections,
630 * but put in background and no more new connections).
631 */
Damien Miller96507ef2001-11-12 10:52:25 +1100632 /* Restore tty modes. */
633 leave_raw_mode();
634
635 /* Stop listening for new connections. */
636 channel_stop_listening();
637
638 snprintf(string, sizeof string,
639 "%c& [backgrounded]\n", escape_char);
640 buffer_append(berr, string, strlen(string));
641
642 /* Fork into background. */
643 pid = fork();
644 if (pid < 0) {
645 error("fork: %.100s", strerror(errno));
646 continue;
647 }
648 if (pid != 0) { /* This is the parent. */
649 /* The parent just exits. */
650 exit(0);
651 }
652 /* The child continues serving connections. */
653 if (compat20) {
654 buffer_append(bin, "\004", 1);
655 /* fake EOF on stdin */
656 return -1;
657 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +1000658 /*
659 * Sending SSH_CMSG_EOF alone does not always appear
660 * to be enough. So we try to send an EOF character
661 * first.
662 */
663 packet_start(SSH_CMSG_STDIN_DATA);
664 packet_put_string("\004", 1);
665 packet_send();
666 /* Close stdin. */
667 stdin_eof = 1;
668 if (buffer_len(bin) == 0) {
669 packet_start(SSH_CMSG_EOF);
670 packet_send();
671 }
672 }
Damien Miller96507ef2001-11-12 10:52:25 +1100673 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000674
675 case '?':
676 snprintf(string, sizeof string,
677"%c?\r\n\
678Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000679%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +1000680%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000681%cC - open a command line\r\n\
682%cR - Request rekey (SSH protocol 2 only)\r\n\
683%c^Z - suspend ssh\r\n\
684%c# - list forwarded connections\r\n\
685%c& - background ssh (when waiting for connections to terminate)\r\n\
686%c? - this message\r\n\
687%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000688(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +1000689 escape_char, escape_char, escape_char, escape_char,
690 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +1000691 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000692 buffer_append(berr, string, strlen(string));
693 continue;
694
695 case '#':
696 snprintf(string, sizeof string, "%c#\r\n", escape_char);
697 buffer_append(berr, string, strlen(string));
698 s = channel_open_message();
699 buffer_append(berr, s, strlen(s));
700 xfree(s);
701 continue;
702
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000703 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +0000704 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000705 continue;
706
Damien Millerad833b32000-08-23 10:46:23 +1000707 default:
708 if (ch != escape_char) {
709 buffer_put_char(bin, escape_char);
710 bytes++;
711 }
712 /* Escaped characters fall through here */
713 break;
714 }
715 } else {
716 /*
717 * The previous character was not an escape char. Check if this
718 * is an escape.
719 */
720 if (last_was_cr && ch == escape_char) {
721 /* It is. Set the flag and continue to next character. */
722 escape_pending = 1;
723 continue;
724 }
725 }
726
727 /*
728 * Normal character. Record whether it was a newline,
729 * and append it to the buffer.
730 */
731 last_was_cr = (ch == '\r' || ch == '\n');
732 buffer_put_char(bin, ch);
733 bytes++;
734 }
735 return bytes;
736}
737
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static void
Damien Miller1383bd82000-04-06 12:32:37 +1000739client_process_input(fd_set * readset)
740{
Damien Miller166fca82000-04-20 07:42:21 +1000741 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000742 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000743
Damien Miller95def091999-11-25 00:26:21 +1100744 /* Read input from stdin. */
745 if (FD_ISSET(fileno(stdin), readset)) {
746 /* Read as much as possible. */
747 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000748 if (len < 0 && (errno == EAGAIN || errno == EINTR))
749 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100750 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100751 /*
752 * Received EOF or error. They are treated
753 * similarly, except that an error message is printed
754 * if it was an error condition.
755 */
Damien Miller95def091999-11-25 00:26:21 +1100756 if (len < 0) {
757 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
758 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100759 }
760 /* Mark that we have seen EOF. */
761 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100762 /*
763 * Send an EOF message to the server unless there is
764 * data in the buffer. If there is data in the
765 * buffer, no message will be sent now. Code
766 * elsewhere will send the EOF when the buffer
767 * becomes empty if stdin_eof is set.
768 */
Damien Miller95def091999-11-25 00:26:21 +1100769 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 packet_start(SSH_CMSG_EOF);
771 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100772 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000773 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +1100774 /*
775 * Normal successful read, and no escape character.
776 * Just append the data to buffer.
777 */
Damien Miller95def091999-11-25 00:26:21 +1100778 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100779 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100780 /*
781 * Normal, successful read. But we have an escape character
782 * and have to process the characters one by one.
783 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000784 if (process_escapes(&stdin_buffer, &stdout_buffer,
785 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000786 return;
Damien Miller95def091999-11-25 00:26:21 +1100787 }
788 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Damien Miller95def091999-11-25 00:26:21 +1100792client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793{
Damien Miller95def091999-11-25 00:26:21 +1100794 int len;
795 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Miller95def091999-11-25 00:26:21 +1100797 /* Write buffered output to stdout. */
798 if (FD_ISSET(fileno(stdout), writeset)) {
799 /* Write as much data as possible. */
800 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100801 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100802 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000803 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100804 len = 0;
805 else {
Damien Miller5428f641999-11-25 11:54:57 +1100806 /*
807 * An error or EOF was encountered. Put an
808 * error message to stderr buffer.
809 */
Damien Miller95def091999-11-25 00:26:21 +1100810 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
811 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100812 quit_pending = 1;
813 return;
814 }
815 }
816 /* Consume printed data from the buffer. */
817 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000818 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 }
Damien Miller95def091999-11-25 00:26:21 +1100820 /* Write buffered output to stderr. */
821 if (FD_ISSET(fileno(stderr), writeset)) {
822 /* Write as much data as possible. */
823 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100824 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100825 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000826 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100827 len = 0;
828 else {
Damien Miller5428f641999-11-25 11:54:57 +1100829 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100830 quit_pending = 1;
831 return;
832 }
833 }
834 /* Consume printed characters from the buffer. */
835 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000836 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100837 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838}
839
Damien Miller5428f641999-11-25 11:54:57 +1100840/*
Damien Millerb38eff82000-04-01 11:09:21 +1000841 * Get packets from the connection input buffer, and process them as long as
842 * there are packets available.
843 *
844 * Any unknown packets received during the actual
845 * session cause the session to terminate. This is
846 * intended to make debugging easier since no
847 * confirmations are sent. Any compatible protocol
848 * extensions must be negotiated during the
849 * preparatory phase.
850 */
851
Ben Lindstrombba81212001-06-25 05:01:22 +0000852static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000853client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000854{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000855 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000856}
857
Damien Millerad833b32000-08-23 10:46:23 +1000858/* scan buf[] for '~' before sending data to the peer */
859
Ben Lindstrombba81212001-06-25 05:01:22 +0000860static int
Damien Millerad833b32000-08-23 10:46:23 +1000861simple_escape_filter(Channel *c, char *buf, int len)
862{
863 /* XXX we assume c->extended is writeable */
864 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
865}
866
Ben Lindstrombba81212001-06-25 05:01:22 +0000867static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000868client_channel_closed(int id, void *arg)
869{
870 if (id != session_ident)
871 error("client_channel_closed: id %d != session_ident %d",
872 id, session_ident);
Damien Miller3ec27592001-10-12 11:35:04 +1000873 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000874 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +1000875 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000876}
877
Damien Millerb38eff82000-04-01 11:09:21 +1000878/*
Damien Miller5428f641999-11-25 11:54:57 +1100879 * Implements the interactive session with the server. This is called after
880 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000881 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
882 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +1100883 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Damien Miller4af51302000-04-16 11:18:38 +1000885int
Damien Millerad833b32000-08-23 10:46:23 +1000886client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887{
Damien Miller5e953212001-01-30 09:14:00 +1100888 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100889 double start_time, total_time;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000890 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +1100891 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
Damien Miller95def091999-11-25 00:26:21 +1100895 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Damien Miller95def091999-11-25 00:26:21 +1100897 /* Initialize variables. */
898 escape_pending = 0;
899 last_was_cr = 1;
900 exit_status = -1;
901 stdin_eof = 0;
902 buffer_high = 64 * 1024;
903 connection_in = packet_get_connection_in();
904 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100905 max_fd = MAX(connection_in, connection_out);
906
907 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000908 /* enable nonblocking unless tty */
909 if (!isatty(fileno(stdin)))
910 set_nonblock(fileno(stdin));
911 if (!isatty(fileno(stdout)))
912 set_nonblock(fileno(stdout));
913 if (!isatty(fileno(stderr)))
914 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100915 max_fd = MAX(max_fd, fileno(stdin));
916 max_fd = MAX(max_fd, fileno(stdout));
917 max_fd = MAX(max_fd, fileno(stderr));
918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 stdin_bytes = 0;
920 stdout_bytes = 0;
921 stderr_bytes = 0;
922 quit_pending = 0;
923 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924
Damien Miller95def091999-11-25 00:26:21 +1100925 /* Initialize buffers. */
926 buffer_init(&stdin_buffer);
927 buffer_init(&stdout_buffer);
928 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929
Damien Millerb38eff82000-04-01 11:09:21 +1000930 client_init_dispatch();
931
Ben Lindstromf49dbff2002-12-23 02:01:55 +0000932 /*
933 * Set signal handlers, (e.g. to restore non-blocking mode)
934 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
935 */
936 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
937 signal(SIGINT, signal_handler);
938 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
939 signal(SIGQUIT, signal_handler);
940 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
941 signal(SIGTERM, signal_handler);
Damien Miller95def091999-11-25 00:26:21 +1100942 if (have_pty)
943 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944
Damien Miller95def091999-11-25 00:26:21 +1100945 if (have_pty)
946 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947
Ben Lindstrom5b828322001-02-09 01:34:36 +0000948 if (compat20) {
949 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000950 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +0000951 channel_register_filter(session_ident,
952 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000953 if (session_ident != -1)
954 channel_register_cleanup(session_ident,
955 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000956 } else {
957 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000958 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000959 }
Damien Millerad833b32000-08-23 10:46:23 +1000960
Damien Miller95def091999-11-25 00:26:21 +1100961 /* Main loop of the client for the interactive session mode. */
962 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller5428f641999-11-25 11:54:57 +1100964 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100965 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000967 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000968 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000969
970 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000971
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000972 if (rekeying) {
973 debug("rekeying in progress");
974 } else {
975 /*
976 * Make packets of buffered stdin data, and buffer
977 * them for sending to the server.
978 */
979 if (!compat20)
980 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000982 /*
983 * Make packets from buffered channel data, and
984 * enqueue them for sending to the server.
985 */
986 if (packet_not_very_much_data_to_write())
987 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000989 /*
990 * Check if the window size has changed, and buffer a
991 * message about it to the server if so.
992 */
993 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000995 if (quit_pending)
996 break;
997 }
Damien Miller5428f641999-11-25 11:54:57 +1100998 /*
999 * Wait until we have something to do (something becomes
1000 * available on one of the descriptors).
1001 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001002 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001003 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001004 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Damien Miller95def091999-11-25 00:26:21 +11001006 if (quit_pending)
1007 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001009 /* Do channel operations unless rekeying in progress. */
1010 if (!rekeying) {
1011 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001012 if (need_rekeying || packet_need_rekeying()) {
1013 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001014 xxx_kex->done = 0;
1015 kex_send_kexinit(xxx_kex);
1016 need_rekeying = 0;
1017 }
1018 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019
Damien Miller1383bd82000-04-06 12:32:37 +10001020 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001021 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Damien Miller1383bd82000-04-06 12:32:37 +10001023 if (quit_pending)
1024 break;
1025
1026 if (!compat20) {
1027 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001028 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001029 /*
1030 * Process output to stdout and stderr. Output to
1031 * the connection is processed elsewhere (above).
1032 */
Damien Miller5e953212001-01-30 09:14:00 +11001033 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001034 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller5428f641999-11-25 11:54:57 +11001036 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001037 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001038 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 }
Damien Miller5e953212001-01-30 09:14:00 +11001040 if (readset)
1041 xfree(readset);
1042 if (writeset)
1043 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
Damien Miller95def091999-11-25 00:26:21 +11001045 /* Terminate the session. */
1046
1047 /* Stop watching for window change. */
1048 if (have_pty)
1049 signal(SIGWINCH, SIG_DFL);
1050
Ben Lindstrom601e4362001-06-21 03:19:23 +00001051 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001052
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001053 if (have_pty)
1054 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001055
1056 /* restore blocking io */
1057 if (!isatty(fileno(stdin)))
1058 unset_nonblock(fileno(stdin));
1059 if (!isatty(fileno(stdout)))
1060 unset_nonblock(fileno(stdout));
1061 if (!isatty(fileno(stderr)))
1062 unset_nonblock(fileno(stderr));
1063
Damien Millerd6965512003-12-17 16:31:53 +11001064 /*
1065 * If there was no shell or command requested, there will be no remote
1066 * exit status to be returned. In that case, clear error code if the
1067 * connection was deliberately terminated at this end.
1068 */
1069 if (no_shell_flag && received_signal == SIGTERM) {
1070 received_signal = 0;
1071 exit_status = 0;
1072 }
1073
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001074 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001075 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001076
1077 /*
1078 * In interactive mode (with pseudo tty) display a message indicating
1079 * that the connection has been closed.
1080 */
1081 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1082 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1083 buffer_append(&stderr_buffer, buf, strlen(buf));
1084 }
1085
Damien Miller95def091999-11-25 00:26:21 +11001086 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001087 while (buffer_len(&stdout_buffer) > 0) {
1088 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001089 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001090 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001091 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001092 break;
1093 }
Damien Miller95def091999-11-25 00:26:21 +11001094 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001095 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001096 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097
Damien Miller95def091999-11-25 00:26:21 +11001098 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001099 while (buffer_len(&stderr_buffer) > 0) {
1100 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001101 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001102 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001103 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001104 break;
1105 }
Damien Miller95def091999-11-25 00:26:21 +11001106 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001107 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001108 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109
Damien Miller95def091999-11-25 00:26:21 +11001110 /* Clear and free any buffers. */
1111 memset(buf, 0, sizeof(buf));
1112 buffer_free(&stdin_buffer);
1113 buffer_free(&stdout_buffer);
1114 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Damien Miller95def091999-11-25 00:26:21 +11001116 /* Report bytes transferred, and transfer rates. */
1117 total_time = get_current_time() - start_time;
1118 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001119 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001120 if (total_time > 0)
1121 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001122 stdin_bytes / total_time, stdout_bytes / total_time,
1123 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001124
1125 /* Return the exit status of the program. */
1126 debug("Exit status %d", exit_status);
1127 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128}
Damien Millerb38eff82000-04-01 11:09:21 +10001129
1130/*********/
1131
Ben Lindstrombba81212001-06-25 05:01:22 +00001132static void
Damien Miller630d6f42002-01-22 23:17:30 +11001133client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001134{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001135 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001136 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001137 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001138 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001139 memset(data, 0, data_len);
1140 xfree(data);
1141}
Ben Lindstrombba81212001-06-25 05:01:22 +00001142static void
Damien Miller630d6f42002-01-22 23:17:30 +11001143client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001144{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001145 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001146 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001147 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001148 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001149 memset(data, 0, data_len);
1150 xfree(data);
1151}
Ben Lindstrombba81212001-06-25 05:01:22 +00001152static void
Damien Miller630d6f42002-01-22 23:17:30 +11001153client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001154{
Damien Millerb38eff82000-04-01 11:09:21 +10001155 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001156 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001157 /* Acknowledge the exit. */
1158 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1159 packet_send();
1160 /*
1161 * Must wait for packet to be sent since we are
1162 * exiting the loop.
1163 */
1164 packet_write_wait();
1165 /* Flag that we want to exit. */
1166 quit_pending = 1;
1167}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001168static void
1169client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1170{
1171 Channel *c = NULL;
1172 int remote_id, sock;
1173
1174 /* Read the remote channel number from the message. */
1175 remote_id = packet_get_int();
1176 packet_check_eom();
1177
1178 /*
1179 * Get a connection to the local authentication agent (this may again
1180 * get forwarded).
1181 */
1182 sock = ssh_get_authentication_socket();
1183
1184 /*
1185 * If we could not connect the agent, send an error message back to
1186 * the server. This should never happen unless the agent dies,
1187 * because authentication forwarding is only enabled if we have an
1188 * agent.
1189 */
1190 if (sock >= 0) {
1191 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1192 -1, 0, 0, 0, "authentication agent connection", 1);
1193 c->remote_id = remote_id;
1194 c->force_drain = 1;
1195 }
1196 if (c == NULL) {
1197 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1198 packet_put_int(remote_id);
1199 } else {
1200 /* Send a confirmation to the remote host. */
1201 debug("Forwarding authentication connection.");
1202 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1203 packet_put_int(remote_id);
1204 packet_put_int(c->self);
1205 }
1206 packet_send();
1207}
Damien Millerb38eff82000-04-01 11:09:21 +10001208
Ben Lindstrombba81212001-06-25 05:01:22 +00001209static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001210client_request_forwarded_tcpip(const char *request_type, int rchan)
1211{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001212 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001213 char *listen_address, *originator_address;
1214 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001215 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216
1217 /* Get rest of the packet */
1218 listen_address = packet_get_string(NULL);
1219 listen_port = packet_get_int();
1220 originator_address = packet_get_string(NULL);
1221 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001222 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001223
1224 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1225 listen_address, listen_port, originator_address, originator_port);
1226
Ben Lindstrom173e6462001-07-04 05:15:15 +00001227 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001228 if (sock < 0) {
1229 xfree(originator_address);
1230 xfree(listen_address);
1231 return NULL;
1232 }
1233 c = channel_new("forwarded-tcpip",
1234 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1235 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001236 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001237 xfree(originator_address);
1238 xfree(listen_address);
1239 return c;
1240}
1241
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001242static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001243client_request_x11(const char *request_type, int rchan)
1244{
1245 Channel *c = NULL;
1246 char *originator;
1247 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001248 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001249
1250 if (!options.forward_x11) {
1251 error("Warning: ssh server tried X11 forwarding.");
1252 error("Warning: this is probably a break in attempt by a malicious server.");
1253 return NULL;
1254 }
1255 originator = packet_get_string(NULL);
1256 if (datafellows & SSH_BUG_X11FWD) {
1257 debug2("buggy server: x11 request w/o originator_port");
1258 originator_port = 0;
1259 } else {
1260 originator_port = packet_get_int();
1261 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001262 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001263 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001264 debug("client_request_x11: request from %s %d", originator,
1265 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001266 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001267 sock = x11_connect_display();
1268 if (sock < 0)
1269 return NULL;
1270 c = channel_new("x11",
1271 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001272 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001273 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001274 return c;
1275}
1276
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001277static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001278client_request_agent(const char *request_type, int rchan)
1279{
1280 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001281 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001282
1283 if (!options.forward_agent) {
1284 error("Warning: ssh server tried agent forwarding.");
1285 error("Warning: this is probably a break in attempt by a malicious server.");
1286 return NULL;
1287 }
1288 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001289 if (sock < 0)
1290 return NULL;
1291 c = channel_new("authentication agent connection",
1292 SSH_CHANNEL_OPEN, sock, sock, -1,
1293 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001294 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001295 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001296 return c;
1297}
1298
Damien Millerbd483e72000-04-30 10:00:53 +10001299/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001300static void
Damien Miller630d6f42002-01-22 23:17:30 +11001301client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001302{
1303 Channel *c = NULL;
1304 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001305 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001306 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001307
1308 ctype = packet_get_string(&len);
1309 rchan = packet_get_int();
1310 rwindow = packet_get_int();
1311 rmaxpack = packet_get_int();
1312
Damien Millere247cc42000-05-07 12:03:14 +10001313 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001314 ctype, rchan, rwindow, rmaxpack);
1315
Damien Miller0bc1bd82000-11-13 22:57:25 +11001316 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1317 c = client_request_forwarded_tcpip(ctype, rchan);
1318 } else if (strcmp(ctype, "x11") == 0) {
1319 c = client_request_x11(ctype, rchan);
1320 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1321 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001322 }
1323/* XXX duplicate : */
1324 if (c != NULL) {
1325 debug("confirm %s", ctype);
1326 c->remote_id = rchan;
1327 c->remote_window = rwindow;
1328 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001329 if (c->type != SSH_CHANNEL_CONNECTING) {
1330 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1331 packet_put_int(c->remote_id);
1332 packet_put_int(c->self);
1333 packet_put_int(c->local_window);
1334 packet_put_int(c->local_maxpacket);
1335 packet_send();
1336 }
Damien Millerbd483e72000-04-30 10:00:53 +10001337 } else {
1338 debug("failure %s", ctype);
1339 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1340 packet_put_int(rchan);
1341 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001342 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001343 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001344 packet_put_cstring("");
1345 }
Damien Millerbd483e72000-04-30 10:00:53 +10001346 packet_send();
1347 }
1348 xfree(ctype);
1349}
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Miller630d6f42002-01-22 23:17:30 +11001351client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001352{
1353 Channel *c = NULL;
1354 int id, reply, success = 0;
1355 char *rtype;
1356
1357 id = packet_get_int();
1358 rtype = packet_get_string(NULL);
1359 reply = packet_get_char();
1360
1361 debug("client_input_channel_req: channel %d rtype %s reply %d",
1362 id, rtype, reply);
1363
1364 if (session_ident == -1) {
1365 error("client_input_channel_req: no channel %d", session_ident);
1366 } else if (id != session_ident) {
1367 error("client_input_channel_req: channel %d: wrong channel: %d",
1368 session_ident, id);
1369 }
1370 c = channel_lookup(id);
1371 if (c == NULL) {
1372 error("client_input_channel_req: channel %d: unknown channel", id);
1373 } else if (strcmp(rtype, "exit-status") == 0) {
1374 success = 1;
1375 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001376 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001377 }
1378 if (reply) {
1379 packet_start(success ?
1380 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1381 packet_put_int(c->remote_id);
1382 packet_send();
1383 }
1384 xfree(rtype);
1385}
Damien Millerc3fa4072002-01-22 23:21:58 +11001386static void
1387client_input_global_request(int type, u_int32_t seq, void *ctxt)
1388{
1389 char *rtype;
1390 int want_reply;
1391 int success = 0;
1392
1393 rtype = packet_get_string(NULL);
1394 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001395 debug("client_input_global_request: rtype %s want_reply %d",
1396 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001397 if (want_reply) {
1398 packet_start(success ?
1399 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1400 packet_send();
1401 packet_write_wait();
1402 }
1403 xfree(rtype);
1404}
Damien Millerbd483e72000-04-30 10:00:53 +10001405
Ben Lindstrombba81212001-06-25 05:01:22 +00001406static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001407client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001408{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001409 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001410
Damien Miller1383bd82000-04-06 12:32:37 +10001411 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1412 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1413 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1414 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001415 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001416 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1417 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001418 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001419 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001420 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001421
1422 /* rekeying */
1423 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001424
1425 /* global request reply messages */
1426 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1427 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001428}
Ben Lindstrombba81212001-06-25 05:01:22 +00001429static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001430client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001431{
1432 dispatch_init(NULL);
1433 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1434 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1435 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001436 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1437 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1438 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001439 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1440 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1441 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001442
1443 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10001444 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11001445 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1446 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001447}
Ben Lindstrombba81212001-06-25 05:01:22 +00001448static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001449client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001450{
1451 client_init_dispatch_13();
1452 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1453 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1454}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001455static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001456client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001457{
Damien Miller1383bd82000-04-06 12:32:37 +10001458 if (compat20)
1459 client_init_dispatch_20();
1460 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001461 client_init_dispatch_13();
1462 else
1463 client_init_dispatch_15();
1464}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001465
1466/* client specific fatal cleanup */
1467void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001468cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001469{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001470 leave_raw_mode();
1471 leave_non_blocking();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001472 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001473}