blob: 67b9dfcea8b30a1e373c824085e3ba950bb041af [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 Millerd6965512003-12-17 16:31:53 +110062RCSID("$OpenBSD: clientloop.c,v 1.116 2003/12/09 23:45:32 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"
Ben Lindstromae8e2d32001-04-14 23:13:02 +000082#include "sshtty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +000083#include "misc.h"
Ben Lindstrom5589f4b2002-03-22 03:24:32 +000084#include "readpass.h"
Damien Miller69b69aa2000-10-28 14:19:58 +110085
86/* import options */
87extern Options options;
88
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089/* Flag indicating that stdin should be redirected from /dev/null. */
90extern int stdin_null_flag;
91
Damien Millerd6965512003-12-17 16:31:53 +110092/* Flag indicating that no shell has been requested */
93extern int no_shell_flag;
94
Damien Miller5428f641999-11-25 11:54:57 +110095/*
96 * Name of the host we are connecting to. This is the name given on the
97 * command line, or the HostName specified for the user-supplied name in a
98 * configuration file.
99 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100extern char *host;
101
Damien Miller5428f641999-11-25 11:54:57 +1100102/*
103 * Flag to indicate that we have received a window change signal which has
104 * not yet been processed. This will cause a message indicating the new
105 * window size to be sent to the server a little later. This is volatile
106 * because this is updated in a signal handler.
107 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000108static volatile sig_atomic_t received_window_change_signal = 0;
109static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
112static int in_non_blocking_mode = 0;
113
114/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000115static int quit_pending; /* Set to non-zero to quit the client loop. */
116static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100117static int escape_pending; /* Last character was the escape character */
118static int last_was_cr; /* Last character was a newline. */
119static int exit_status; /* Used to store the exit status of the command. */
120static int stdin_eof; /* EOF has been encountered on standard error. */
121static Buffer stdin_buffer; /* Buffer for stdin data. */
122static Buffer stdout_buffer; /* Buffer for stdout data. */
123static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000124static u_long stdin_bytes, stdout_bytes, stderr_bytes;
125static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100126static int connection_in; /* Connection to server (input). */
127static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000128static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000129static int session_closed = 0; /* In SSH2: login session closed. */
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 Miller5428f641999-11-25 11:54:57 +1100316/*
317 * Waits until the client can do something (some data becomes available on
318 * one of the file descriptors).
319 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static void
Damien Miller5e953212001-01-30 09:14:00 +1100322client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000323 int *maxfdp, int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Damien Miller5e953212001-01-30 09:14:00 +1100325 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000326 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller1383bd82000-04-06 12:32:37 +1000328 if (!compat20) {
329 /* Read from the connection, unless our buffers are full. */
330 if (buffer_len(&stdout_buffer) < buffer_high &&
331 buffer_len(&stderr_buffer) < buffer_high &&
332 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100333 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000334 /*
335 * Read from stdin, unless we have seen EOF or have very much
336 * buffered data to send to the server.
337 */
338 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100339 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000340
341 /* Select stdout/stderr if have data in buffer. */
342 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100343 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000344 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100345 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000346 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000347 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000348 if (session_closed && !channel_still_open() &&
349 !packet_have_data_to_write()) {
350 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100351 memset(*readsetp, 0, *nallocp);
352 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000353 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000354 } else {
355 FD_SET(connection_in, *readsetp);
356 }
Damien Miller1383bd82000-04-06 12:32:37 +1000357 }
358
Damien Miller95def091999-11-25 00:26:21 +1100359 /* Select server connection if have data to write to the server. */
360 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100361 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100362
Damien Miller5428f641999-11-25 11:54:57 +1100363 /*
364 * Wait for something to happen. This will suspend the process until
365 * some selected descriptor can be read, written, or has some other
366 * event pending. Note: if you want to implement SSH_MSG_IGNORE
367 * messages to fool traffic analysis, this might be the place to do
368 * it: just have a random timeout for the select, and send a random
369 * SSH_MSG_IGNORE packet when the timeout expires.
370 */
Damien Miller95def091999-11-25 00:26:21 +1100371
Damien Miller5e953212001-01-30 09:14:00 +1100372 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100373 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000374
375 /*
376 * We have to clear the select masks, because we return.
377 * We have to return, because the mainloop checks for the flags
378 * set by the signal handlers.
379 */
Damien Miller79faeff2001-11-12 11:06:32 +1100380 memset(*readsetp, 0, *nallocp);
381 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000382
Damien Miller95def091999-11-25 00:26:21 +1100383 if (errno == EINTR)
384 return;
385 /* Note: we might still have data in the buffers. */
386 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
387 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100388 quit_pending = 1;
389 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390}
391
Ben Lindstrombba81212001-06-25 05:01:22 +0000392static void
Damien Millerad833b32000-08-23 10:46:23 +1000393client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394{
Damien Miller95def091999-11-25 00:26:21 +1100395 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller95def091999-11-25 00:26:21 +1100397 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000398 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000399 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000400 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000401 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402
Damien Miller95def091999-11-25 00:26:21 +1100403 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller5428f641999-11-25 11:54:57 +1100405 /*
406 * Free (and clear) the buffer to reduce the amount of data that gets
407 * written to swap.
408 */
Damien Millerad833b32000-08-23 10:46:23 +1000409 buffer_free(bin);
410 buffer_free(bout);
411 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* Save old window size. */
414 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Send the suspend signal to the program itself. */
417 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 /* Check if the window size has changed. */
420 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
421 (oldws.ws_row != newws.ws_row ||
Damien Miller9f0f5c62001-12-21 14:45:46 +1100422 oldws.ws_col != newws.ws_col ||
423 oldws.ws_xpixel != newws.ws_xpixel ||
424 oldws.ws_ypixel != newws.ws_ypixel))
Damien Miller95def091999-11-25 00:26:21 +1100425 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Damien Miller95def091999-11-25 00:26:21 +1100427 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000428 buffer_init(bin);
429 buffer_init(bout);
430 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller95def091999-11-25 00:26:21 +1100432 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}
434
Ben Lindstrombba81212001-06-25 05:01:22 +0000435static void
Damien Miller1383bd82000-04-06 12:32:37 +1000436client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437{
Damien Miller1383bd82000-04-06 12:32:37 +1000438 int len;
439 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Miller5428f641999-11-25 11:54:57 +1100441 /*
442 * Read input from the server, and add any such data to the buffer of
443 * the packet subsystem.
444 */
Damien Miller95def091999-11-25 00:26:21 +1100445 if (FD_ISSET(connection_in, readset)) {
446 /* Read as much as possible. */
447 len = read(connection_in, buf, sizeof(buf));
448 if (len == 0) {
449 /* Received EOF. The remote host has closed the connection. */
450 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
451 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453 quit_pending = 1;
454 return;
Damien Miller95def091999-11-25 00:26:21 +1100455 }
Damien Miller5428f641999-11-25 11:54:57 +1100456 /*
457 * There is a kernel bug on Solaris that causes select to
458 * sometimes wake up even though there is no data available.
459 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000460 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100461 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
Damien Miller95def091999-11-25 00:26:21 +1100463 if (len < 0) {
464 /* An error has encountered. Perhaps there is a network problem. */
465 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
466 host, strerror(errno));
467 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100468 quit_pending = 1;
469 return;
470 }
471 packet_process_incoming(buf, len);
472 }
Damien Miller1383bd82000-04-06 12:32:37 +1000473}
474
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000475static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000476process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000477{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000478 void (*handler)(int);
479 char *s, *cmd;
480 u_short fwd_port, fwd_host_port;
481 char buf[1024], sfwd_port[6], sfwd_host_port[6];
482 int local = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000483
484 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000485 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000486 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000487 if (s == NULL)
488 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000489 while (*s && isspace(*s))
490 s++;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000491 if (*s == 0)
492 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000493 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
Damien Miller996acd22003-04-09 20:59:48 +1000494 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000495 goto out;
496 }
497 if (s[1] == 'L')
498 local = 1;
499 if (!local && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000500 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000501 goto out;
502 }
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000503 s += 2;
504 while (*s && isspace(*s))
505 s++;
506
507 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
508 sfwd_port, buf, sfwd_host_port) != 3 &&
509 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]",
510 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Miller996acd22003-04-09 20:59:48 +1000511 logit("Bad forwarding specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000512 goto out;
513 }
514 if ((fwd_port = a2port(sfwd_port)) == 0 ||
515 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000516 logit("Bad forwarding port(s).");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000517 goto out;
518 }
519 if (local) {
Ben Lindstrom681d9322002-03-22 03:53:00 +0000520 if (channel_setup_local_fwd_listener(fwd_port, buf,
521 fwd_host_port, options.gateway_ports) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000522 logit("Port forwarding failed.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000523 goto out;
524 }
525 } else
526 channel_request_remote_forwarding(fwd_port, buf,
527 fwd_host_port);
Damien Miller996acd22003-04-09 20:59:48 +1000528 logit("Forwarding port.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000529out:
530 signal(SIGINT, handler);
531 enter_raw_mode();
532 if (cmd)
533 xfree(cmd);
534}
535
Damien Millerad833b32000-08-23 10:46:23 +1000536/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +0000537static int
Damien Millerad833b32000-08-23 10:46:23 +1000538process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
539{
540 char string[1024];
541 pid_t pid;
542 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000543 u_int i;
544 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000545 char *s;
546
547 for (i = 0; i < len; i++) {
548 /* Get one character at a time. */
549 ch = buf[i];
550
551 if (escape_pending) {
552 /* We have previously seen an escape character. */
553 /* Clear the flag now. */
554 escape_pending = 0;
555
556 /* Process the escaped character. */
557 switch (ch) {
558 case '.':
559 /* Terminate the connection. */
560 snprintf(string, sizeof string, "%c.\r\n", escape_char);
561 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000562
563 quit_pending = 1;
564 return -1;
565
566 case 'Z' - 64:
567 /* Suspend the program. */
568 /* Print a message to that effect to the user. */
569 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
570 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000571
572 /* Restore terminal modes and suspend. */
573 client_suspend_self(bin, bout, berr);
574
575 /* We have been continued. */
576 continue;
577
Damien Miller54c45982003-05-15 10:20:13 +1000578 case 'B':
579 if (compat20) {
580 snprintf(string, sizeof string,
581 "%cB\r\n", escape_char);
582 buffer_append(berr, string,
583 strlen(string));
584 channel_request_start(session_ident,
585 "break", 0);
586 packet_put_int(1000);
587 packet_send();
588 }
589 continue;
590
Ben Lindstromf28f6342001-04-04 02:03:04 +0000591 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000592 if (compat20) {
593 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +1000594 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000595 else
596 need_rekeying = 1;
597 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000598 continue;
599
Damien Millerad833b32000-08-23 10:46:23 +1000600 case '&':
Damien Millerad833b32000-08-23 10:46:23 +1000601 /*
602 * Detach the program (continue to serve connections,
603 * but put in background and no more new connections).
604 */
Damien Miller96507ef2001-11-12 10:52:25 +1100605 /* Restore tty modes. */
606 leave_raw_mode();
607
608 /* Stop listening for new connections. */
609 channel_stop_listening();
610
611 snprintf(string, sizeof string,
612 "%c& [backgrounded]\n", escape_char);
613 buffer_append(berr, string, strlen(string));
614
615 /* Fork into background. */
616 pid = fork();
617 if (pid < 0) {
618 error("fork: %.100s", strerror(errno));
619 continue;
620 }
621 if (pid != 0) { /* This is the parent. */
622 /* The parent just exits. */
623 exit(0);
624 }
625 /* The child continues serving connections. */
626 if (compat20) {
627 buffer_append(bin, "\004", 1);
628 /* fake EOF on stdin */
629 return -1;
630 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +1000631 /*
632 * Sending SSH_CMSG_EOF alone does not always appear
633 * to be enough. So we try to send an EOF character
634 * first.
635 */
636 packet_start(SSH_CMSG_STDIN_DATA);
637 packet_put_string("\004", 1);
638 packet_send();
639 /* Close stdin. */
640 stdin_eof = 1;
641 if (buffer_len(bin) == 0) {
642 packet_start(SSH_CMSG_EOF);
643 packet_send();
644 }
645 }
Damien Miller96507ef2001-11-12 10:52:25 +1100646 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000647
648 case '?':
649 snprintf(string, sizeof string,
650"%c?\r\n\
651Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000652%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +1000653%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000654%cC - open a command line\r\n\
655%cR - Request rekey (SSH protocol 2 only)\r\n\
656%c^Z - suspend ssh\r\n\
657%c# - list forwarded connections\r\n\
658%c& - background ssh (when waiting for connections to terminate)\r\n\
659%c? - this message\r\n\
660%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000661(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +1000662 escape_char, escape_char, escape_char, escape_char,
663 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +1000664 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000665 buffer_append(berr, string, strlen(string));
666 continue;
667
668 case '#':
669 snprintf(string, sizeof string, "%c#\r\n", escape_char);
670 buffer_append(berr, string, strlen(string));
671 s = channel_open_message();
672 buffer_append(berr, s, strlen(s));
673 xfree(s);
674 continue;
675
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000676 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +0000677 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000678 continue;
679
Damien Millerad833b32000-08-23 10:46:23 +1000680 default:
681 if (ch != escape_char) {
682 buffer_put_char(bin, escape_char);
683 bytes++;
684 }
685 /* Escaped characters fall through here */
686 break;
687 }
688 } else {
689 /*
690 * The previous character was not an escape char. Check if this
691 * is an escape.
692 */
693 if (last_was_cr && ch == escape_char) {
694 /* It is. Set the flag and continue to next character. */
695 escape_pending = 1;
696 continue;
697 }
698 }
699
700 /*
701 * Normal character. Record whether it was a newline,
702 * and append it to the buffer.
703 */
704 last_was_cr = (ch == '\r' || ch == '\n');
705 buffer_put_char(bin, ch);
706 bytes++;
707 }
708 return bytes;
709}
710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Miller1383bd82000-04-06 12:32:37 +1000712client_process_input(fd_set * readset)
713{
Damien Miller166fca82000-04-20 07:42:21 +1000714 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000715 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000716
Damien Miller95def091999-11-25 00:26:21 +1100717 /* Read input from stdin. */
718 if (FD_ISSET(fileno(stdin), readset)) {
719 /* Read as much as possible. */
720 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000721 if (len < 0 && (errno == EAGAIN || errno == EINTR))
722 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100723 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100724 /*
725 * Received EOF or error. They are treated
726 * similarly, except that an error message is printed
727 * if it was an error condition.
728 */
Damien Miller95def091999-11-25 00:26:21 +1100729 if (len < 0) {
730 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
731 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100732 }
733 /* Mark that we have seen EOF. */
734 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100735 /*
736 * Send an EOF message to the server unless there is
737 * data in the buffer. If there is data in the
738 * buffer, no message will be sent now. Code
739 * elsewhere will send the EOF when the buffer
740 * becomes empty if stdin_eof is set.
741 */
Damien Miller95def091999-11-25 00:26:21 +1100742 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 packet_start(SSH_CMSG_EOF);
744 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100745 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000746 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +1100747 /*
748 * Normal successful read, and no escape character.
749 * Just append the data to buffer.
750 */
Damien Miller95def091999-11-25 00:26:21 +1100751 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100752 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100753 /*
754 * Normal, successful read. But we have an escape character
755 * and have to process the characters one by one.
756 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000757 if (process_escapes(&stdin_buffer, &stdout_buffer,
758 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000759 return;
Damien Miller95def091999-11-25 00:26:21 +1100760 }
761 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762}
763
Ben Lindstrombba81212001-06-25 05:01:22 +0000764static void
Damien Miller95def091999-11-25 00:26:21 +1100765client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766{
Damien Miller95def091999-11-25 00:26:21 +1100767 int len;
768 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769
Damien Miller95def091999-11-25 00:26:21 +1100770 /* Write buffered output to stdout. */
771 if (FD_ISSET(fileno(stdout), writeset)) {
772 /* Write as much data as possible. */
773 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100774 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100775 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000776 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100777 len = 0;
778 else {
Damien Miller5428f641999-11-25 11:54:57 +1100779 /*
780 * An error or EOF was encountered. Put an
781 * error message to stderr buffer.
782 */
Damien Miller95def091999-11-25 00:26:21 +1100783 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
784 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100785 quit_pending = 1;
786 return;
787 }
788 }
789 /* Consume printed data from the buffer. */
790 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000791 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792 }
Damien Miller95def091999-11-25 00:26:21 +1100793 /* Write buffered output to stderr. */
794 if (FD_ISSET(fileno(stderr), writeset)) {
795 /* Write as much data as possible. */
796 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100797 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100798 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000799 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100800 len = 0;
801 else {
Damien Miller5428f641999-11-25 11:54:57 +1100802 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100803 quit_pending = 1;
804 return;
805 }
806 }
807 /* Consume printed characters from the buffer. */
808 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000809 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100810 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}
812
Damien Miller5428f641999-11-25 11:54:57 +1100813/*
Damien Millerb38eff82000-04-01 11:09:21 +1000814 * Get packets from the connection input buffer, and process them as long as
815 * there are packets available.
816 *
817 * Any unknown packets received during the actual
818 * session cause the session to terminate. This is
819 * intended to make debugging easier since no
820 * confirmations are sent. Any compatible protocol
821 * extensions must be negotiated during the
822 * preparatory phase.
823 */
824
Ben Lindstrombba81212001-06-25 05:01:22 +0000825static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000826client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000827{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000828 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000829}
830
Damien Millerad833b32000-08-23 10:46:23 +1000831/* scan buf[] for '~' before sending data to the peer */
832
Ben Lindstrombba81212001-06-25 05:01:22 +0000833static int
Damien Millerad833b32000-08-23 10:46:23 +1000834simple_escape_filter(Channel *c, char *buf, int len)
835{
836 /* XXX we assume c->extended is writeable */
837 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
838}
839
Ben Lindstrombba81212001-06-25 05:01:22 +0000840static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000841client_channel_closed(int id, void *arg)
842{
843 if (id != session_ident)
844 error("client_channel_closed: id %d != session_ident %d",
845 id, session_ident);
Damien Miller3ec27592001-10-12 11:35:04 +1000846 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000847 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +1000848 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000849}
850
Damien Millerb38eff82000-04-01 11:09:21 +1000851/*
Damien Miller5428f641999-11-25 11:54:57 +1100852 * Implements the interactive session with the server. This is called after
853 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000854 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
855 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +1100856 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857
Damien Miller4af51302000-04-16 11:18:38 +1000858int
Damien Millerad833b32000-08-23 10:46:23 +1000859client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860{
Damien Miller5e953212001-01-30 09:14:00 +1100861 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100862 double start_time, total_time;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000863 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +1100864 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865
Damien Miller95def091999-11-25 00:26:21 +1100866 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867
Damien Miller95def091999-11-25 00:26:21 +1100868 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869
Damien Miller95def091999-11-25 00:26:21 +1100870 /* Initialize variables. */
871 escape_pending = 0;
872 last_was_cr = 1;
873 exit_status = -1;
874 stdin_eof = 0;
875 buffer_high = 64 * 1024;
876 connection_in = packet_get_connection_in();
877 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100878 max_fd = MAX(connection_in, connection_out);
879
880 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000881 /* enable nonblocking unless tty */
882 if (!isatty(fileno(stdin)))
883 set_nonblock(fileno(stdin));
884 if (!isatty(fileno(stdout)))
885 set_nonblock(fileno(stdout));
886 if (!isatty(fileno(stderr)))
887 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100888 max_fd = MAX(max_fd, fileno(stdin));
889 max_fd = MAX(max_fd, fileno(stdout));
890 max_fd = MAX(max_fd, fileno(stderr));
891 }
Damien Miller95def091999-11-25 00:26:21 +1100892 stdin_bytes = 0;
893 stdout_bytes = 0;
894 stderr_bytes = 0;
895 quit_pending = 0;
896 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller95def091999-11-25 00:26:21 +1100898 /* Initialize buffers. */
899 buffer_init(&stdin_buffer);
900 buffer_init(&stdout_buffer);
901 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Millerb38eff82000-04-01 11:09:21 +1000903 client_init_dispatch();
904
Ben Lindstromf49dbff2002-12-23 02:01:55 +0000905 /*
906 * Set signal handlers, (e.g. to restore non-blocking mode)
907 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
908 */
909 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
910 signal(SIGINT, signal_handler);
911 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
912 signal(SIGQUIT, signal_handler);
913 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
914 signal(SIGTERM, signal_handler);
Damien Miller95def091999-11-25 00:26:21 +1100915 if (have_pty)
916 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Damien Miller95def091999-11-25 00:26:21 +1100918 if (have_pty)
919 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Ben Lindstrom5b828322001-02-09 01:34:36 +0000921 if (compat20) {
922 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000923 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +0000924 channel_register_filter(session_ident,
925 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000926 if (session_ident != -1)
927 channel_register_cleanup(session_ident,
928 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000929 } else {
930 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000931 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000932 }
Damien Millerad833b32000-08-23 10:46:23 +1000933
Damien Miller95def091999-11-25 00:26:21 +1100934 /* Main loop of the client for the interactive session mode. */
935 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Miller5428f641999-11-25 11:54:57 +1100937 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100938 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000940 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000941 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000942
943 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000944
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000945 if (rekeying) {
946 debug("rekeying in progress");
947 } else {
948 /*
949 * Make packets of buffered stdin data, and buffer
950 * them for sending to the server.
951 */
952 if (!compat20)
953 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000955 /*
956 * Make packets from buffered channel data, and
957 * enqueue them for sending to the server.
958 */
959 if (packet_not_very_much_data_to_write())
960 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000962 /*
963 * Check if the window size has changed, and buffer a
964 * message about it to the server if so.
965 */
966 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000968 if (quit_pending)
969 break;
970 }
Damien Miller5428f641999-11-25 11:54:57 +1100971 /*
972 * Wait until we have something to do (something becomes
973 * available on one of the descriptors).
974 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000975 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000976 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000977 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 if (quit_pending)
980 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000982 /* Do channel operations unless rekeying in progress. */
983 if (!rekeying) {
984 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +1000985 if (need_rekeying || packet_need_rekeying()) {
986 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000987 xxx_kex->done = 0;
988 kex_send_kexinit(xxx_kex);
989 need_rekeying = 0;
990 }
991 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller1383bd82000-04-06 12:32:37 +1000993 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +1100994 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995
Damien Miller1383bd82000-04-06 12:32:37 +1000996 if (quit_pending)
997 break;
998
999 if (!compat20) {
1000 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001001 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001002 /*
1003 * Process output to stdout and stderr. Output to
1004 * the connection is processed elsewhere (above).
1005 */
Damien Miller5e953212001-01-30 09:14:00 +11001006 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001007 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller5428f641999-11-25 11:54:57 +11001009 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001010 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001011 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 }
Damien Miller5e953212001-01-30 09:14:00 +11001013 if (readset)
1014 xfree(readset);
1015 if (writeset)
1016 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Miller95def091999-11-25 00:26:21 +11001018 /* Terminate the session. */
1019
1020 /* Stop watching for window change. */
1021 if (have_pty)
1022 signal(SIGWINCH, SIG_DFL);
1023
Ben Lindstrom601e4362001-06-21 03:19:23 +00001024 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001025
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001026 if (have_pty)
1027 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001028
1029 /* restore blocking io */
1030 if (!isatty(fileno(stdin)))
1031 unset_nonblock(fileno(stdin));
1032 if (!isatty(fileno(stdout)))
1033 unset_nonblock(fileno(stdout));
1034 if (!isatty(fileno(stderr)))
1035 unset_nonblock(fileno(stderr));
1036
Damien Millerd6965512003-12-17 16:31:53 +11001037 /*
1038 * If there was no shell or command requested, there will be no remote
1039 * exit status to be returned. In that case, clear error code if the
1040 * connection was deliberately terminated at this end.
1041 */
1042 if (no_shell_flag && received_signal == SIGTERM) {
1043 received_signal = 0;
1044 exit_status = 0;
1045 }
1046
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001047 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001048 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001049
1050 /*
1051 * In interactive mode (with pseudo tty) display a message indicating
1052 * that the connection has been closed.
1053 */
1054 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1055 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1056 buffer_append(&stderr_buffer, buf, strlen(buf));
1057 }
1058
Damien Miller95def091999-11-25 00:26:21 +11001059 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001060 while (buffer_len(&stdout_buffer) > 0) {
1061 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001062 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001063 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001064 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001065 break;
1066 }
Damien Miller95def091999-11-25 00:26:21 +11001067 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001068 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001069 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070
Damien Miller95def091999-11-25 00:26:21 +11001071 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001072 while (buffer_len(&stderr_buffer) > 0) {
1073 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001074 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001075 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001076 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001077 break;
1078 }
Damien Miller95def091999-11-25 00:26:21 +11001079 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001080 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001081 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082
Damien Miller95def091999-11-25 00:26:21 +11001083 /* Clear and free any buffers. */
1084 memset(buf, 0, sizeof(buf));
1085 buffer_free(&stdin_buffer);
1086 buffer_free(&stdout_buffer);
1087 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088
Damien Miller95def091999-11-25 00:26:21 +11001089 /* Report bytes transferred, and transfer rates. */
1090 total_time = get_current_time() - start_time;
1091 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001092 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001093 if (total_time > 0)
1094 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001095 stdin_bytes / total_time, stdout_bytes / total_time,
1096 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001097
1098 /* Return the exit status of the program. */
1099 debug("Exit status %d", exit_status);
1100 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101}
Damien Millerb38eff82000-04-01 11:09:21 +10001102
1103/*********/
1104
Ben Lindstrombba81212001-06-25 05:01:22 +00001105static void
Damien Miller630d6f42002-01-22 23:17:30 +11001106client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001107{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001108 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001109 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001110 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001111 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001112 memset(data, 0, data_len);
1113 xfree(data);
1114}
Ben Lindstrombba81212001-06-25 05:01:22 +00001115static void
Damien Miller630d6f42002-01-22 23:17:30 +11001116client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001117{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001118 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001119 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001120 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001121 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001122 memset(data, 0, data_len);
1123 xfree(data);
1124}
Ben Lindstrombba81212001-06-25 05:01:22 +00001125static void
Damien Miller630d6f42002-01-22 23:17:30 +11001126client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001127{
Damien Millerb38eff82000-04-01 11:09:21 +10001128 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001129 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001130 /* Acknowledge the exit. */
1131 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1132 packet_send();
1133 /*
1134 * Must wait for packet to be sent since we are
1135 * exiting the loop.
1136 */
1137 packet_write_wait();
1138 /* Flag that we want to exit. */
1139 quit_pending = 1;
1140}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001141static void
1142client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1143{
1144 Channel *c = NULL;
1145 int remote_id, sock;
1146
1147 /* Read the remote channel number from the message. */
1148 remote_id = packet_get_int();
1149 packet_check_eom();
1150
1151 /*
1152 * Get a connection to the local authentication agent (this may again
1153 * get forwarded).
1154 */
1155 sock = ssh_get_authentication_socket();
1156
1157 /*
1158 * If we could not connect the agent, send an error message back to
1159 * the server. This should never happen unless the agent dies,
1160 * because authentication forwarding is only enabled if we have an
1161 * agent.
1162 */
1163 if (sock >= 0) {
1164 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1165 -1, 0, 0, 0, "authentication agent connection", 1);
1166 c->remote_id = remote_id;
1167 c->force_drain = 1;
1168 }
1169 if (c == NULL) {
1170 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1171 packet_put_int(remote_id);
1172 } else {
1173 /* Send a confirmation to the remote host. */
1174 debug("Forwarding authentication connection.");
1175 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1176 packet_put_int(remote_id);
1177 packet_put_int(c->self);
1178 }
1179 packet_send();
1180}
Damien Millerb38eff82000-04-01 11:09:21 +10001181
Ben Lindstrombba81212001-06-25 05:01:22 +00001182static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001183client_request_forwarded_tcpip(const char *request_type, int rchan)
1184{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001185 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001186 char *listen_address, *originator_address;
1187 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001188 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001189
1190 /* Get rest of the packet */
1191 listen_address = packet_get_string(NULL);
1192 listen_port = packet_get_int();
1193 originator_address = packet_get_string(NULL);
1194 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001195 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001196
1197 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1198 listen_address, listen_port, originator_address, originator_port);
1199
Ben Lindstrom173e6462001-07-04 05:15:15 +00001200 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001201 if (sock < 0) {
1202 xfree(originator_address);
1203 xfree(listen_address);
1204 return NULL;
1205 }
1206 c = channel_new("forwarded-tcpip",
1207 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1208 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001209 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001210 xfree(originator_address);
1211 xfree(listen_address);
1212 return c;
1213}
1214
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001215static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216client_request_x11(const char *request_type, int rchan)
1217{
1218 Channel *c = NULL;
1219 char *originator;
1220 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001221 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001222
1223 if (!options.forward_x11) {
1224 error("Warning: ssh server tried X11 forwarding.");
1225 error("Warning: this is probably a break in attempt by a malicious server.");
1226 return NULL;
1227 }
1228 originator = packet_get_string(NULL);
1229 if (datafellows & SSH_BUG_X11FWD) {
1230 debug2("buggy server: x11 request w/o originator_port");
1231 originator_port = 0;
1232 } else {
1233 originator_port = packet_get_int();
1234 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001235 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001236 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001237 debug("client_request_x11: request from %s %d", originator,
1238 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001239 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001240 sock = x11_connect_display();
1241 if (sock < 0)
1242 return NULL;
1243 c = channel_new("x11",
1244 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001245 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001246 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001247 return c;
1248}
1249
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001250static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001251client_request_agent(const char *request_type, int rchan)
1252{
1253 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001254 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001255
1256 if (!options.forward_agent) {
1257 error("Warning: ssh server tried agent forwarding.");
1258 error("Warning: this is probably a break in attempt by a malicious server.");
1259 return NULL;
1260 }
1261 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001262 if (sock < 0)
1263 return NULL;
1264 c = channel_new("authentication agent connection",
1265 SSH_CHANNEL_OPEN, sock, sock, -1,
1266 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001267 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001268 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001269 return c;
1270}
1271
Damien Millerbd483e72000-04-30 10:00:53 +10001272/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001273static void
Damien Miller630d6f42002-01-22 23:17:30 +11001274client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001275{
1276 Channel *c = NULL;
1277 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001278 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001279 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001280
1281 ctype = packet_get_string(&len);
1282 rchan = packet_get_int();
1283 rwindow = packet_get_int();
1284 rmaxpack = packet_get_int();
1285
Damien Millere247cc42000-05-07 12:03:14 +10001286 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001287 ctype, rchan, rwindow, rmaxpack);
1288
Damien Miller0bc1bd82000-11-13 22:57:25 +11001289 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1290 c = client_request_forwarded_tcpip(ctype, rchan);
1291 } else if (strcmp(ctype, "x11") == 0) {
1292 c = client_request_x11(ctype, rchan);
1293 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1294 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001295 }
1296/* XXX duplicate : */
1297 if (c != NULL) {
1298 debug("confirm %s", ctype);
1299 c->remote_id = rchan;
1300 c->remote_window = rwindow;
1301 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001302 if (c->type != SSH_CHANNEL_CONNECTING) {
1303 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1304 packet_put_int(c->remote_id);
1305 packet_put_int(c->self);
1306 packet_put_int(c->local_window);
1307 packet_put_int(c->local_maxpacket);
1308 packet_send();
1309 }
Damien Millerbd483e72000-04-30 10:00:53 +10001310 } else {
1311 debug("failure %s", ctype);
1312 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1313 packet_put_int(rchan);
1314 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001315 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001316 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001317 packet_put_cstring("");
1318 }
Damien Millerbd483e72000-04-30 10:00:53 +10001319 packet_send();
1320 }
1321 xfree(ctype);
1322}
Ben Lindstrombba81212001-06-25 05:01:22 +00001323static void
Damien Miller630d6f42002-01-22 23:17:30 +11001324client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001325{
1326 Channel *c = NULL;
1327 int id, reply, success = 0;
1328 char *rtype;
1329
1330 id = packet_get_int();
1331 rtype = packet_get_string(NULL);
1332 reply = packet_get_char();
1333
1334 debug("client_input_channel_req: channel %d rtype %s reply %d",
1335 id, rtype, reply);
1336
1337 if (session_ident == -1) {
1338 error("client_input_channel_req: no channel %d", session_ident);
1339 } else if (id != session_ident) {
1340 error("client_input_channel_req: channel %d: wrong channel: %d",
1341 session_ident, id);
1342 }
1343 c = channel_lookup(id);
1344 if (c == NULL) {
1345 error("client_input_channel_req: channel %d: unknown channel", id);
1346 } else if (strcmp(rtype, "exit-status") == 0) {
1347 success = 1;
1348 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001349 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001350 }
1351 if (reply) {
1352 packet_start(success ?
1353 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1354 packet_put_int(c->remote_id);
1355 packet_send();
1356 }
1357 xfree(rtype);
1358}
Damien Millerc3fa4072002-01-22 23:21:58 +11001359static void
1360client_input_global_request(int type, u_int32_t seq, void *ctxt)
1361{
1362 char *rtype;
1363 int want_reply;
1364 int success = 0;
1365
1366 rtype = packet_get_string(NULL);
1367 want_reply = packet_get_char();
1368 debug("client_input_global_request: rtype %s want_reply %d", rtype, want_reply);
1369 if (want_reply) {
1370 packet_start(success ?
1371 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1372 packet_send();
1373 packet_write_wait();
1374 }
1375 xfree(rtype);
1376}
Damien Millerbd483e72000-04-30 10:00:53 +10001377
Ben Lindstrombba81212001-06-25 05:01:22 +00001378static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001379client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001380{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001381 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001382
Damien Miller1383bd82000-04-06 12:32:37 +10001383 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1384 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1385 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1386 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001387 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001388 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1389 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001390 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001391 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001392 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001393
1394 /* rekeying */
1395 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001396
1397 /* global request reply messages */
1398 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1399 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001400}
Ben Lindstrombba81212001-06-25 05:01:22 +00001401static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001402client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001403{
1404 dispatch_init(NULL);
1405 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1406 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1407 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001408 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1409 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1410 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001411 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1412 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1413 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001414
1415 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10001416 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11001417 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1418 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001419}
Ben Lindstrombba81212001-06-25 05:01:22 +00001420static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001421client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001422{
1423 client_init_dispatch_13();
1424 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1425 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1426}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001427static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001428client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001429{
Damien Miller1383bd82000-04-06 12:32:37 +10001430 if (compat20)
1431 client_init_dispatch_20();
1432 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001433 client_init_dispatch_13();
1434 else
1435 client_init_dispatch_15();
1436}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001437
1438/* client specific fatal cleanup */
1439void
Darren Tucker3e33cec2003-10-02 16:12:36 +10001440cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001441{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001442 leave_raw_mode();
1443 leave_non_blocking();
Darren Tucker3e33cec2003-10-02 16:12:36 +10001444 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001445}