blob: dbac4091f8f9839a1ebcbdb59e31cbf39ae0fd12 [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"
Ben Lindstromeda98a72002-03-22 03:35:48 +000062RCSID("$OpenBSD: clientloop.c,v 1.98 2002/03/21 16:58:13 markus 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 Miller5428f641999-11-25 11:54:57 +110092/*
93 * Name of the host we are connecting to. This is the name given on the
94 * command line, or the HostName specified for the user-supplied name in a
95 * configuration file.
96 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097extern char *host;
98
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * Flag to indicate that we have received a window change signal which has
101 * not yet been processed. This will cause a message indicating the new
102 * window size to be sent to the server a little later. This is volatile
103 * because this is updated in a signal handler.
104 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000105static volatile sig_atomic_t received_window_change_signal = 0;
106static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
109static int in_non_blocking_mode = 0;
110
111/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000112static int quit_pending; /* Set to non-zero to quit the client loop. */
113static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100114static int escape_pending; /* Last character was the escape character */
115static int last_was_cr; /* Last character was a newline. */
116static int exit_status; /* Used to store the exit status of the command. */
117static int stdin_eof; /* EOF has been encountered on standard error. */
118static Buffer stdin_buffer; /* Buffer for stdin data. */
119static Buffer stdout_buffer; /* Buffer for stdout data. */
120static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121static u_long stdin_bytes, stdout_bytes, stderr_bytes;
122static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100123static int connection_in; /* Connection to server (input). */
124static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000125static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000126static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Ben Lindstrombba81212001-06-25 05:01:22 +0000128static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000129int session_ident = -1;
130
Ben Lindstromf28f6342001-04-04 02:03:04 +0000131/*XXX*/
132extern Kex *xxx_kex;
133
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134/* Restores stdin to blocking mode. */
135
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000137leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Miller95def091999-11-25 00:26:21 +1100139 if (in_non_blocking_mode) {
140 (void) fcntl(fileno(stdin), F_SETFL, 0);
141 in_non_blocking_mode = 0;
142 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Miller95def091999-11-25 00:26:21 +1100146/* Puts stdin terminal in non-blocking mode. */
147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000149enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Damien Miller95def091999-11-25 00:26:21 +1100151 in_non_blocking_mode = 1;
152 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
153 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154}
155
Damien Miller5428f641999-11-25 11:54:57 +1100156/*
157 * Signal handler for the window change signal (SIGWINCH). This just sets a
158 * flag indicating that the window has changed.
159 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static void
Damien Miller95def091999-11-25 00:26:21 +1100162window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 received_window_change_signal = 1;
165 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Signal handler for signals that cause the program to terminate. These
170 * signals must be trapped to restore terminal modes.
171 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Ben Lindstrombba81212001-06-25 05:01:22 +0000173static void
Damien Miller95def091999-11-25 00:26:21 +1100174signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000176 received_signal = sig;
177 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
181 * Returns current time in seconds from Jan 1, 1970 with the maximum
182 * available resolution.
183 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000186get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187{
Damien Miller95def091999-11-25 00:26:21 +1100188 struct timeval tv;
189 gettimeofday(&tv, NULL);
190 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Miller5428f641999-11-25 11:54:57 +1100193/*
194 * This is called when the interactive is entered. This checks if there is
195 * an EOF coming on stdin. We must check this explicitly, as select() does
196 * not appear to wake up when redirecting from /dev/null.
197 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000200client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller95def091999-11-25 00:26:21 +1100202 int len;
203 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller5428f641999-11-25 11:54:57 +1100205 /*
206 * If standard input is to be "redirected from /dev/null", we simply
207 * mark that we have seen an EOF and send an EOF message to the
208 * server. Otherwise, we try to read a single character; it appears
209 * that for some files, such /dev/null, select() never wakes up for
210 * read for this descriptor, which means that we never get EOF. This
211 * way we will get the EOF if stdin comes from /dev/null or similar.
212 */
Damien Miller95def091999-11-25 00:26:21 +1100213 if (stdin_null_flag) {
214 /* Fake EOF on stdin. */
215 debug("Sending eof.");
216 stdin_eof = 1;
217 packet_start(SSH_CMSG_EOF);
218 packet_send();
219 } else {
Damien Miller95def091999-11-25 00:26:21 +1100220 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Check for immediate EOF on stdin. */
223 len = read(fileno(stdin), buf, 1);
224 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100225 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100226 debug("Sending eof.");
227 stdin_eof = 1;
228 packet_start(SSH_CMSG_EOF);
229 packet_send();
230 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100231 /*
232 * Got data. We must store the data in the buffer,
233 * and also process it as an escape character if
234 * appropriate.
235 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000236 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100237 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000238 else
Damien Miller95def091999-11-25 00:26:21 +1100239 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100240 }
Damien Miller95def091999-11-25 00:26:21 +1100241 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller5428f641999-11-25 11:54:57 +1100246/*
247 * Make packets from buffered stdin data, and buffer them for sending to the
248 * connection.
249 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Ben Lindstrombba81212001-06-25 05:01:22 +0000251static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000252client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000254 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller95def091999-11-25 00:26:21 +1100256 /* Send buffered stdin data to the server. */
257 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100258 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100259 len = buffer_len(&stdin_buffer);
260 /* Keep the packets at reasonable size. */
261 if (len > packet_get_maxsize())
262 len = packet_get_maxsize();
263 packet_start(SSH_CMSG_STDIN_DATA);
264 packet_put_string(buffer_ptr(&stdin_buffer), len);
265 packet_send();
266 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000267 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100268 /* If we have a pending EOF, send it now. */
269 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
270 packet_start(SSH_CMSG_EOF);
271 packet_send();
272 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274}
275
Damien Miller5428f641999-11-25 11:54:57 +1100276/*
277 * Checks if the client window has changed, and sends a packet about it to
278 * the server if so. The actual change is detected elsewhere (by a software
279 * interrupt on Unix); this just checks the flag and sends a message if
280 * appropriate.
281 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000284client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285{
Damien Miller1383bd82000-04-06 12:32:37 +1000286 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller1383bd82000-04-06 12:32:37 +1000288 if (! received_window_change_signal)
289 return;
290 /** XXX race */
291 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller1383bd82000-04-06 12:32:37 +1000293 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
294 return;
295
Damien Millerd3444942000-09-30 14:20:03 +1100296 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000297
298 if (compat20) {
299 channel_request_start(session_ident, "window-change", 0);
300 packet_put_int(ws.ws_col);
301 packet_put_int(ws.ws_row);
302 packet_put_int(ws.ws_xpixel);
303 packet_put_int(ws.ws_ypixel);
304 packet_send();
305 } else {
306 packet_start(SSH_CMSG_WINDOW_SIZE);
307 packet_put_int(ws.ws_row);
308 packet_put_int(ws.ws_col);
309 packet_put_int(ws.ws_xpixel);
310 packet_put_int(ws.ws_ypixel);
311 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313}
314
Damien Miller5428f641999-11-25 11:54:57 +1100315/*
316 * Waits until the client can do something (some data becomes available on
317 * one of the file descriptors).
318 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Ben Lindstrombba81212001-06-25 05:01:22 +0000320static void
Damien Miller5e953212001-01-30 09:14:00 +1100321client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000322 int *maxfdp, int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323{
Damien Miller5e953212001-01-30 09:14:00 +1100324 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000325 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller1383bd82000-04-06 12:32:37 +1000327 if (!compat20) {
328 /* Read from the connection, unless our buffers are full. */
329 if (buffer_len(&stdout_buffer) < buffer_high &&
330 buffer_len(&stderr_buffer) < buffer_high &&
331 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100332 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000333 /*
334 * Read from stdin, unless we have seen EOF or have very much
335 * buffered data to send to the server.
336 */
337 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100338 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000339
340 /* Select stdout/stderr if have data in buffer. */
341 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100342 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000343 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100344 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000345 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000346 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000347 if (session_closed && !channel_still_open() &&
348 !packet_have_data_to_write()) {
349 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100350 memset(*readsetp, 0, *nallocp);
351 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000352 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000353 } else {
354 FD_SET(connection_in, *readsetp);
355 }
Damien Miller1383bd82000-04-06 12:32:37 +1000356 }
357
Damien Miller95def091999-11-25 00:26:21 +1100358 /* Select server connection if have data to write to the server. */
359 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100360 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100361
Damien Miller5428f641999-11-25 11:54:57 +1100362 /*
363 * Wait for something to happen. This will suspend the process until
364 * some selected descriptor can be read, written, or has some other
365 * event pending. Note: if you want to implement SSH_MSG_IGNORE
366 * messages to fool traffic analysis, this might be the place to do
367 * it: just have a random timeout for the select, and send a random
368 * SSH_MSG_IGNORE packet when the timeout expires.
369 */
Damien Miller95def091999-11-25 00:26:21 +1100370
Damien Miller5e953212001-01-30 09:14:00 +1100371 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100372 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000373
374 /*
375 * We have to clear the select masks, because we return.
376 * We have to return, because the mainloop checks for the flags
377 * set by the signal handlers.
378 */
Damien Miller79faeff2001-11-12 11:06:32 +1100379 memset(*readsetp, 0, *nallocp);
380 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000381
Damien Miller95def091999-11-25 00:26:21 +1100382 if (errno == EINTR)
383 return;
384 /* Note: we might still have data in the buffers. */
385 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
386 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100387 quit_pending = 1;
388 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389}
390
Ben Lindstrombba81212001-06-25 05:01:22 +0000391static void
Damien Millerad833b32000-08-23 10:46:23 +1000392client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393{
Damien Miller95def091999-11-25 00:26:21 +1100394 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000397 if (buffer_len(bout) > 0)
398 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
399 if (buffer_len(berr) > 0)
400 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Damien Miller95def091999-11-25 00:26:21 +1100402 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Damien Miller5428f641999-11-25 11:54:57 +1100404 /*
405 * Free (and clear) the buffer to reduce the amount of data that gets
406 * written to swap.
407 */
Damien Millerad833b32000-08-23 10:46:23 +1000408 buffer_free(bin);
409 buffer_free(bout);
410 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Save old window size. */
413 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Send the suspend signal to the program itself. */
416 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 /* Check if the window size has changed. */
419 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
420 (oldws.ws_row != newws.ws_row ||
Damien Miller9f0f5c62001-12-21 14:45:46 +1100421 oldws.ws_col != newws.ws_col ||
422 oldws.ws_xpixel != newws.ws_xpixel ||
423 oldws.ws_ypixel != newws.ws_ypixel))
Damien Miller95def091999-11-25 00:26:21 +1100424 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425
Damien Miller95def091999-11-25 00:26:21 +1100426 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000427 buffer_init(bin);
428 buffer_init(bout);
429 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Damien Miller95def091999-11-25 00:26:21 +1100431 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Ben Lindstrombba81212001-06-25 05:01:22 +0000434static void
Damien Miller1383bd82000-04-06 12:32:37 +1000435client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller1383bd82000-04-06 12:32:37 +1000437 int len;
438 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller5428f641999-11-25 11:54:57 +1100440 /*
441 * Read input from the server, and add any such data to the buffer of
442 * the packet subsystem.
443 */
Damien Miller95def091999-11-25 00:26:21 +1100444 if (FD_ISSET(connection_in, readset)) {
445 /* Read as much as possible. */
446 len = read(connection_in, buf, sizeof(buf));
447 if (len == 0) {
448 /* Received EOF. The remote host has closed the connection. */
449 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
450 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 quit_pending = 1;
453 return;
Damien Miller95def091999-11-25 00:26:21 +1100454 }
Damien Miller5428f641999-11-25 11:54:57 +1100455 /*
456 * There is a kernel bug on Solaris that causes select to
457 * sometimes wake up even though there is no data available.
458 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000459 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100460 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Miller95def091999-11-25 00:26:21 +1100462 if (len < 0) {
463 /* An error has encountered. Perhaps there is a network problem. */
464 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
465 host, strerror(errno));
466 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100467 quit_pending = 1;
468 return;
469 }
470 packet_process_incoming(buf, len);
471 }
Damien Miller1383bd82000-04-06 12:32:37 +1000472}
473
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000474static void
475process_cmdline(Buffer *bin, Buffer *bout, Buffer *berr)
476{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000477 void (*handler)(int);
478 char *s, *cmd;
479 u_short fwd_port, fwd_host_port;
480 char buf[1024], sfwd_port[6], sfwd_host_port[6];
481 int local = 0;
482 int n;
483
484 leave_raw_mode();
485 handler = signal(SIGINT, SIG_IGN);
486 s = read_passphrase("\r\nssh> ", RP_ECHO);
487 if (s == NULL)
488 goto out;
489 cmd = s;
490
491 while (*s && isspace(*s))
492 s++;
493
494 if (*s == 0)
495 goto out;
496
497 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
498 log("Invalid command");
499 goto out;
500 }
501 if (s[1] == 'L')
502 local = 1;
503 if (!local && !compat20) {
504 log("Not supported for SSH protocol version 1");
505 goto out;
506 }
507
508 s += 2;
509 while (*s && isspace(*s))
510 s++;
511
512 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
513 sfwd_port, buf, sfwd_host_port) != 3 &&
514 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]",
515 sfwd_port, buf, sfwd_host_port) != 3) {
516 log("Bad forwarding specification");
517 goto out;
518 }
519 if ((fwd_port = a2port(sfwd_port)) == 0 ||
520 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
521 log("Bad forwarding port(s)");
522 goto out;
523 }
524 if (local) {
525 n = channel_setup_local_fwd_listener(fwd_port, buf,
526 fwd_host_port, options.gateway_ports);
527 if (n <= 0) {
528 log("Port forwarding failed");
529 goto out;
530 }
531 } else
532 channel_request_remote_forwarding(fwd_port, buf,
533 fwd_host_port);
534 log("Forwarding port");
535out:
536 signal(SIGINT, handler);
537 enter_raw_mode();
538 if (cmd)
539 xfree(cmd);
540}
541
Damien Millerad833b32000-08-23 10:46:23 +1000542/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +0000543static int
Damien Millerad833b32000-08-23 10:46:23 +1000544process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
545{
546 char string[1024];
547 pid_t pid;
548 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000549 u_int i;
550 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000551 char *s;
552
553 for (i = 0; i < len; i++) {
554 /* Get one character at a time. */
555 ch = buf[i];
556
557 if (escape_pending) {
558 /* We have previously seen an escape character. */
559 /* Clear the flag now. */
560 escape_pending = 0;
561
562 /* Process the escaped character. */
563 switch (ch) {
564 case '.':
565 /* Terminate the connection. */
566 snprintf(string, sizeof string, "%c.\r\n", escape_char);
567 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000568
569 quit_pending = 1;
570 return -1;
571
572 case 'Z' - 64:
573 /* Suspend the program. */
574 /* Print a message to that effect to the user. */
575 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
576 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000577
578 /* Restore terminal modes and suspend. */
579 client_suspend_self(bin, bout, berr);
580
581 /* We have been continued. */
582 continue;
583
Ben Lindstromf28f6342001-04-04 02:03:04 +0000584 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000585 if (compat20) {
586 if (datafellows & SSH_BUG_NOREKEY)
587 log("Server does not support re-keying");
588 else
589 need_rekeying = 1;
590 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000591 continue;
592
Damien Millerad833b32000-08-23 10:46:23 +1000593 case '&':
Damien Millerad833b32000-08-23 10:46:23 +1000594 /*
595 * Detach the program (continue to serve connections,
596 * but put in background and no more new connections).
597 */
Damien Miller96507ef2001-11-12 10:52:25 +1100598 /* Restore tty modes. */
599 leave_raw_mode();
600
601 /* Stop listening for new connections. */
602 channel_stop_listening();
603
604 snprintf(string, sizeof string,
605 "%c& [backgrounded]\n", escape_char);
606 buffer_append(berr, string, strlen(string));
607
608 /* Fork into background. */
609 pid = fork();
610 if (pid < 0) {
611 error("fork: %.100s", strerror(errno));
612 continue;
613 }
614 if (pid != 0) { /* This is the parent. */
615 /* The parent just exits. */
616 exit(0);
617 }
618 /* The child continues serving connections. */
619 if (compat20) {
620 buffer_append(bin, "\004", 1);
621 /* fake EOF on stdin */
622 return -1;
623 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +1000624 /*
625 * Sending SSH_CMSG_EOF alone does not always appear
626 * to be enough. So we try to send an EOF character
627 * first.
628 */
629 packet_start(SSH_CMSG_STDIN_DATA);
630 packet_put_string("\004", 1);
631 packet_send();
632 /* Close stdin. */
633 stdin_eof = 1;
634 if (buffer_len(bin) == 0) {
635 packet_start(SSH_CMSG_EOF);
636 packet_send();
637 }
638 }
Damien Miller96507ef2001-11-12 10:52:25 +1100639 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000640
641 case '?':
642 snprintf(string, sizeof string,
643"%c?\r\n\
644Supported escape sequences:\r\n\
645~. - terminate connection\r\n\
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000646~C - open a command line\r\n\
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000647~R - Request rekey (SSH protocol 2 only)\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000648~^Z - suspend ssh\r\n\
649~# - list forwarded connections\r\n\
650~& - background ssh (when waiting for connections to terminate)\r\n\
651~? - this message\r\n\
652~~ - send the escape character by typing it twice\r\n\
653(Note that escapes are only recognized immediately after newline.)\r\n",
654 escape_char);
655 buffer_append(berr, string, strlen(string));
656 continue;
657
658 case '#':
659 snprintf(string, sizeof string, "%c#\r\n", escape_char);
660 buffer_append(berr, string, strlen(string));
661 s = channel_open_message();
662 buffer_append(berr, s, strlen(s));
663 xfree(s);
664 continue;
665
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000666 case 'C':
667 process_cmdline(bin, bout, berr);
668 continue;
669
Damien Millerad833b32000-08-23 10:46:23 +1000670 default:
671 if (ch != escape_char) {
672 buffer_put_char(bin, escape_char);
673 bytes++;
674 }
675 /* Escaped characters fall through here */
676 break;
677 }
678 } else {
679 /*
680 * The previous character was not an escape char. Check if this
681 * is an escape.
682 */
683 if (last_was_cr && ch == escape_char) {
684 /* It is. Set the flag and continue to next character. */
685 escape_pending = 1;
686 continue;
687 }
688 }
689
690 /*
691 * Normal character. Record whether it was a newline,
692 * and append it to the buffer.
693 */
694 last_was_cr = (ch == '\r' || ch == '\n');
695 buffer_put_char(bin, ch);
696 bytes++;
697 }
698 return bytes;
699}
700
Ben Lindstrombba81212001-06-25 05:01:22 +0000701static void
Damien Miller1383bd82000-04-06 12:32:37 +1000702client_process_input(fd_set * readset)
703{
Damien Miller166fca82000-04-20 07:42:21 +1000704 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000705 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000706
Damien Miller95def091999-11-25 00:26:21 +1100707 /* Read input from stdin. */
708 if (FD_ISSET(fileno(stdin), readset)) {
709 /* Read as much as possible. */
710 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000711 if (len < 0 && (errno == EAGAIN || errno == EINTR))
712 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100713 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100714 /*
715 * Received EOF or error. They are treated
716 * similarly, except that an error message is printed
717 * if it was an error condition.
718 */
Damien Miller95def091999-11-25 00:26:21 +1100719 if (len < 0) {
720 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
721 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100722 }
723 /* Mark that we have seen EOF. */
724 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100725 /*
726 * Send an EOF message to the server unless there is
727 * data in the buffer. If there is data in the
728 * buffer, no message will be sent now. Code
729 * elsewhere will send the EOF when the buffer
730 * becomes empty if stdin_eof is set.
731 */
Damien Miller95def091999-11-25 00:26:21 +1100732 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 packet_start(SSH_CMSG_EOF);
734 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100735 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000736 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +1100737 /*
738 * Normal successful read, and no escape character.
739 * Just append the data to buffer.
740 */
Damien Miller95def091999-11-25 00:26:21 +1100741 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100742 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100743 /*
744 * Normal, successful read. But we have an escape character
745 * and have to process the characters one by one.
746 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000747 if (process_escapes(&stdin_buffer, &stdout_buffer,
748 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000749 return;
Damien Miller95def091999-11-25 00:26:21 +1100750 }
751 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752}
753
Ben Lindstrombba81212001-06-25 05:01:22 +0000754static void
Damien Miller95def091999-11-25 00:26:21 +1100755client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756{
Damien Miller95def091999-11-25 00:26:21 +1100757 int len;
758 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller95def091999-11-25 00:26:21 +1100760 /* Write buffered output to stdout. */
761 if (FD_ISSET(fileno(stdout), writeset)) {
762 /* Write as much data as possible. */
763 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100764 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100765 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000766 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100767 len = 0;
768 else {
Damien Miller5428f641999-11-25 11:54:57 +1100769 /*
770 * An error or EOF was encountered. Put an
771 * error message to stderr buffer.
772 */
Damien Miller95def091999-11-25 00:26:21 +1100773 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
774 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100775 quit_pending = 1;
776 return;
777 }
778 }
779 /* Consume printed data from the buffer. */
780 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000781 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782 }
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Write buffered output to stderr. */
784 if (FD_ISSET(fileno(stderr), writeset)) {
785 /* Write as much data as possible. */
786 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100787 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100788 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000789 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100790 len = 0;
791 else {
Damien Miller5428f641999-11-25 11:54:57 +1100792 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100793 quit_pending = 1;
794 return;
795 }
796 }
797 /* Consume printed characters from the buffer. */
798 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000799 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100800 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801}
802
Damien Miller5428f641999-11-25 11:54:57 +1100803/*
Damien Millerb38eff82000-04-01 11:09:21 +1000804 * Get packets from the connection input buffer, and process them as long as
805 * there are packets available.
806 *
807 * Any unknown packets received during the actual
808 * session cause the session to terminate. This is
809 * intended to make debugging easier since no
810 * confirmations are sent. Any compatible protocol
811 * extensions must be negotiated during the
812 * preparatory phase.
813 */
814
Ben Lindstrombba81212001-06-25 05:01:22 +0000815static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000816client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000817{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000818 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000819}
820
Damien Millerad833b32000-08-23 10:46:23 +1000821/* scan buf[] for '~' before sending data to the peer */
822
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static int
Damien Millerad833b32000-08-23 10:46:23 +1000824simple_escape_filter(Channel *c, char *buf, int len)
825{
826 /* XXX we assume c->extended is writeable */
827 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
828}
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000831client_channel_closed(int id, void *arg)
832{
833 if (id != session_ident)
834 error("client_channel_closed: id %d != session_ident %d",
835 id, session_ident);
Damien Miller3ec27592001-10-12 11:35:04 +1000836 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000837 session_closed = 1;
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000838 if (in_raw_mode())
Ben Lindstromd3447632001-04-08 18:07:22 +0000839 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000840}
841
Damien Millerb38eff82000-04-01 11:09:21 +1000842/*
Damien Miller5428f641999-11-25 11:54:57 +1100843 * Implements the interactive session with the server. This is called after
844 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000845 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
846 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +1100847 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Miller4af51302000-04-16 11:18:38 +1000849int
Damien Millerad833b32000-08-23 10:46:23 +1000850client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851{
Damien Miller5e953212001-01-30 09:14:00 +1100852 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100853 double start_time, total_time;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000854 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +1100855 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Damien Miller95def091999-11-25 00:26:21 +1100857 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858
Damien Miller95def091999-11-25 00:26:21 +1100859 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Damien Miller95def091999-11-25 00:26:21 +1100861 /* Initialize variables. */
862 escape_pending = 0;
863 last_was_cr = 1;
864 exit_status = -1;
865 stdin_eof = 0;
866 buffer_high = 64 * 1024;
867 connection_in = packet_get_connection_in();
868 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100869 max_fd = MAX(connection_in, connection_out);
870
871 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000872 /* enable nonblocking unless tty */
873 if (!isatty(fileno(stdin)))
874 set_nonblock(fileno(stdin));
875 if (!isatty(fileno(stdout)))
876 set_nonblock(fileno(stdout));
877 if (!isatty(fileno(stderr)))
878 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100879 max_fd = MAX(max_fd, fileno(stdin));
880 max_fd = MAX(max_fd, fileno(stdout));
881 max_fd = MAX(max_fd, fileno(stderr));
882 }
Damien Miller95def091999-11-25 00:26:21 +1100883 stdin_bytes = 0;
884 stdout_bytes = 0;
885 stderr_bytes = 0;
886 quit_pending = 0;
887 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
Damien Miller95def091999-11-25 00:26:21 +1100889 /* Initialize buffers. */
890 buffer_init(&stdin_buffer);
891 buffer_init(&stdout_buffer);
892 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Millerb38eff82000-04-01 11:09:21 +1000894 client_init_dispatch();
895
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Set signal handlers to restore non-blocking mode. */
897 signal(SIGINT, signal_handler);
898 signal(SIGQUIT, signal_handler);
899 signal(SIGTERM, signal_handler);
Damien Miller95def091999-11-25 00:26:21 +1100900 if (have_pty)
901 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller95def091999-11-25 00:26:21 +1100903 if (have_pty)
904 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905
Ben Lindstrom5b828322001-02-09 01:34:36 +0000906 if (compat20) {
907 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000908 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +0000909 channel_register_filter(session_ident,
910 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000911 if (session_ident != -1)
912 channel_register_cleanup(session_ident,
913 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000914 } else {
915 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000916 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000917 }
Damien Millerad833b32000-08-23 10:46:23 +1000918
Damien Miller95def091999-11-25 00:26:21 +1100919 /* Main loop of the client for the interactive session mode. */
920 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921
Damien Miller5428f641999-11-25 11:54:57 +1100922 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100923 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000925 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000926 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000927
928 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000929
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000930 if (rekeying) {
931 debug("rekeying in progress");
932 } else {
933 /*
934 * Make packets of buffered stdin data, and buffer
935 * them for sending to the server.
936 */
937 if (!compat20)
938 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000940 /*
941 * Make packets from buffered channel data, and
942 * enqueue them for sending to the server.
943 */
944 if (packet_not_very_much_data_to_write())
945 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000947 /*
948 * Check if the window size has changed, and buffer a
949 * message about it to the server if so.
950 */
951 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000953 if (quit_pending)
954 break;
955 }
Damien Miller5428f641999-11-25 11:54:57 +1100956 /*
957 * Wait until we have something to do (something becomes
958 * available on one of the descriptors).
959 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000960 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000961 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000962 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller95def091999-11-25 00:26:21 +1100964 if (quit_pending)
965 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000967 /* Do channel operations unless rekeying in progress. */
968 if (!rekeying) {
969 channel_after_select(readset, writeset);
970
971 if (need_rekeying) {
972 debug("user requests rekeying");
973 xxx_kex->done = 0;
974 kex_send_kexinit(xxx_kex);
975 need_rekeying = 0;
976 }
977 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller1383bd82000-04-06 12:32:37 +1000979 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +1100980 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller1383bd82000-04-06 12:32:37 +1000982 if (quit_pending)
983 break;
984
985 if (!compat20) {
986 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +1100987 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +1000988 /*
989 * Process output to stdout and stderr. Output to
990 * the connection is processed elsewhere (above).
991 */
Damien Miller5e953212001-01-30 09:14:00 +1100992 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +1000993 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller5428f641999-11-25 11:54:57 +1100995 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +1100996 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +1100997 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998 }
Damien Miller5e953212001-01-30 09:14:00 +1100999 if (readset)
1000 xfree(readset);
1001 if (writeset)
1002 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Miller95def091999-11-25 00:26:21 +11001004 /* Terminate the session. */
1005
1006 /* Stop watching for window change. */
1007 if (have_pty)
1008 signal(SIGWINCH, SIG_DFL);
1009
Ben Lindstrom601e4362001-06-21 03:19:23 +00001010 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001011
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001012 if (have_pty)
1013 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001014
1015 /* restore blocking io */
1016 if (!isatty(fileno(stdin)))
1017 unset_nonblock(fileno(stdin));
1018 if (!isatty(fileno(stdout)))
1019 unset_nonblock(fileno(stdout));
1020 if (!isatty(fileno(stderr)))
1021 unset_nonblock(fileno(stderr));
1022
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001023 if (received_signal) {
1024 if (in_non_blocking_mode) /* XXX */
1025 leave_non_blocking();
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001026 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001027 }
1028
1029 /*
1030 * In interactive mode (with pseudo tty) display a message indicating
1031 * that the connection has been closed.
1032 */
1033 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1034 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1035 buffer_append(&stderr_buffer, buf, strlen(buf));
1036 }
1037
Damien Miller95def091999-11-25 00:26:21 +11001038 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001039 while (buffer_len(&stdout_buffer) > 0) {
1040 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001041 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001042 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001043 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001044 break;
1045 }
Damien Miller95def091999-11-25 00:26:21 +11001046 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001047 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001048 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049
Damien Miller95def091999-11-25 00:26:21 +11001050 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001051 while (buffer_len(&stderr_buffer) > 0) {
1052 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001053 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001054 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001055 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001056 break;
1057 }
Damien Miller95def091999-11-25 00:26:21 +11001058 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001059 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001060 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061
Damien Miller95def091999-11-25 00:26:21 +11001062 /* Clear and free any buffers. */
1063 memset(buf, 0, sizeof(buf));
1064 buffer_free(&stdin_buffer);
1065 buffer_free(&stdout_buffer);
1066 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller95def091999-11-25 00:26:21 +11001068 /* Report bytes transferred, and transfer rates. */
1069 total_time = get_current_time() - start_time;
1070 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001071 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001072 if (total_time > 0)
1073 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001074 stdin_bytes / total_time, stdout_bytes / total_time,
1075 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001076
1077 /* Return the exit status of the program. */
1078 debug("Exit status %d", exit_status);
1079 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080}
Damien Millerb38eff82000-04-01 11:09:21 +10001081
1082/*********/
1083
Ben Lindstrombba81212001-06-25 05:01:22 +00001084static void
Damien Miller630d6f42002-01-22 23:17:30 +11001085client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001086{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001087 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001088 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001089 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001090 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001091 memset(data, 0, data_len);
1092 xfree(data);
1093}
Ben Lindstrombba81212001-06-25 05:01:22 +00001094static void
Damien Miller630d6f42002-01-22 23:17:30 +11001095client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001096{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001097 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001098 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001099 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001100 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001101 memset(data, 0, data_len);
1102 xfree(data);
1103}
Ben Lindstrombba81212001-06-25 05:01:22 +00001104static void
Damien Miller630d6f42002-01-22 23:17:30 +11001105client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001106{
Damien Millerb38eff82000-04-01 11:09:21 +10001107 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001108 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001109 /* Acknowledge the exit. */
1110 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1111 packet_send();
1112 /*
1113 * Must wait for packet to be sent since we are
1114 * exiting the loop.
1115 */
1116 packet_write_wait();
1117 /* Flag that we want to exit. */
1118 quit_pending = 1;
1119}
1120
Ben Lindstrombba81212001-06-25 05:01:22 +00001121static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001122client_request_forwarded_tcpip(const char *request_type, int rchan)
1123{
1124 Channel* c = NULL;
1125 char *listen_address, *originator_address;
1126 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001127 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001128
1129 /* Get rest of the packet */
1130 listen_address = packet_get_string(NULL);
1131 listen_port = packet_get_int();
1132 originator_address = packet_get_string(NULL);
1133 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001134 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001135
1136 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1137 listen_address, listen_port, originator_address, originator_port);
1138
Ben Lindstrom173e6462001-07-04 05:15:15 +00001139 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001140 if (sock < 0) {
1141 xfree(originator_address);
1142 xfree(listen_address);
1143 return NULL;
1144 }
1145 c = channel_new("forwarded-tcpip",
1146 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1147 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1148 xstrdup(originator_address), 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001149 xfree(originator_address);
1150 xfree(listen_address);
1151 return c;
1152}
1153
Ben Lindstrombba81212001-06-25 05:01:22 +00001154static Channel*
Damien Miller0bc1bd82000-11-13 22:57:25 +11001155client_request_x11(const char *request_type, int rchan)
1156{
1157 Channel *c = NULL;
1158 char *originator;
1159 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001160 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001161
1162 if (!options.forward_x11) {
1163 error("Warning: ssh server tried X11 forwarding.");
1164 error("Warning: this is probably a break in attempt by a malicious server.");
1165 return NULL;
1166 }
1167 originator = packet_get_string(NULL);
1168 if (datafellows & SSH_BUG_X11FWD) {
1169 debug2("buggy server: x11 request w/o originator_port");
1170 originator_port = 0;
1171 } else {
1172 originator_port = packet_get_int();
1173 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001174 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001175 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001176 debug("client_request_x11: request from %s %d", originator,
1177 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001178 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001179 sock = x11_connect_display();
1180 if (sock < 0)
1181 return NULL;
1182 c = channel_new("x11",
1183 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1184 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1185 xstrdup("x11"), 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001186 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001187 return c;
1188}
1189
Ben Lindstrombba81212001-06-25 05:01:22 +00001190static Channel*
Damien Miller0bc1bd82000-11-13 22:57:25 +11001191client_request_agent(const char *request_type, int rchan)
1192{
1193 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001194 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001195
1196 if (!options.forward_agent) {
1197 error("Warning: ssh server tried agent forwarding.");
1198 error("Warning: this is probably a break in attempt by a malicious server.");
1199 return NULL;
1200 }
1201 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001202 if (sock < 0)
1203 return NULL;
1204 c = channel_new("authentication agent connection",
1205 SSH_CHANNEL_OPEN, sock, sock, -1,
1206 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1207 xstrdup("authentication agent connection"), 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001208 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001209 return c;
1210}
1211
Damien Millerbd483e72000-04-30 10:00:53 +10001212/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001213static void
Damien Miller630d6f42002-01-22 23:17:30 +11001214client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001215{
1216 Channel *c = NULL;
1217 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001218 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001219 int rchan;
1220 int rmaxpack;
1221 int rwindow;
1222
1223 ctype = packet_get_string(&len);
1224 rchan = packet_get_int();
1225 rwindow = packet_get_int();
1226 rmaxpack = packet_get_int();
1227
Damien Millere247cc42000-05-07 12:03:14 +10001228 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001229 ctype, rchan, rwindow, rmaxpack);
1230
Damien Miller0bc1bd82000-11-13 22:57:25 +11001231 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1232 c = client_request_forwarded_tcpip(ctype, rchan);
1233 } else if (strcmp(ctype, "x11") == 0) {
1234 c = client_request_x11(ctype, rchan);
1235 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1236 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001237 }
1238/* XXX duplicate : */
1239 if (c != NULL) {
1240 debug("confirm %s", ctype);
1241 c->remote_id = rchan;
1242 c->remote_window = rwindow;
1243 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001244 if (c->type != SSH_CHANNEL_CONNECTING) {
1245 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1246 packet_put_int(c->remote_id);
1247 packet_put_int(c->self);
1248 packet_put_int(c->local_window);
1249 packet_put_int(c->local_maxpacket);
1250 packet_send();
1251 }
Damien Millerbd483e72000-04-30 10:00:53 +10001252 } else {
1253 debug("failure %s", ctype);
1254 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1255 packet_put_int(rchan);
1256 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001257 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001258 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001259 packet_put_cstring("");
1260 }
Damien Millerbd483e72000-04-30 10:00:53 +10001261 packet_send();
1262 }
1263 xfree(ctype);
1264}
Ben Lindstrombba81212001-06-25 05:01:22 +00001265static void
Damien Miller630d6f42002-01-22 23:17:30 +11001266client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001267{
1268 Channel *c = NULL;
1269 int id, reply, success = 0;
1270 char *rtype;
1271
1272 id = packet_get_int();
1273 rtype = packet_get_string(NULL);
1274 reply = packet_get_char();
1275
1276 debug("client_input_channel_req: channel %d rtype %s reply %d",
1277 id, rtype, reply);
1278
1279 if (session_ident == -1) {
1280 error("client_input_channel_req: no channel %d", session_ident);
1281 } else if (id != session_ident) {
1282 error("client_input_channel_req: channel %d: wrong channel: %d",
1283 session_ident, id);
1284 }
1285 c = channel_lookup(id);
1286 if (c == NULL) {
1287 error("client_input_channel_req: channel %d: unknown channel", id);
1288 } else if (strcmp(rtype, "exit-status") == 0) {
1289 success = 1;
1290 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001291 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001292 }
1293 if (reply) {
1294 packet_start(success ?
1295 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1296 packet_put_int(c->remote_id);
1297 packet_send();
1298 }
1299 xfree(rtype);
1300}
Damien Millerc3fa4072002-01-22 23:21:58 +11001301static void
1302client_input_global_request(int type, u_int32_t seq, void *ctxt)
1303{
1304 char *rtype;
1305 int want_reply;
1306 int success = 0;
1307
1308 rtype = packet_get_string(NULL);
1309 want_reply = packet_get_char();
1310 debug("client_input_global_request: rtype %s want_reply %d", rtype, want_reply);
1311 if (want_reply) {
1312 packet_start(success ?
1313 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1314 packet_send();
1315 packet_write_wait();
1316 }
1317 xfree(rtype);
1318}
Damien Millerbd483e72000-04-30 10:00:53 +10001319
Ben Lindstrombba81212001-06-25 05:01:22 +00001320static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001321client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001322{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001323 dispatch_init(&dispatch_protocol_error);
Damien Miller1383bd82000-04-06 12:32:37 +10001324 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1325 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1326 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1327 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001328 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001329 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1330 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001331 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001332 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001333 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001334
1335 /* rekeying */
1336 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller1383bd82000-04-06 12:32:37 +10001337}
Ben Lindstrombba81212001-06-25 05:01:22 +00001338static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001339client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001340{
1341 dispatch_init(NULL);
1342 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1343 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1344 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001345 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1346 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1347 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001348 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1349 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1350 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001351
1352 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1353 &auth_input_open_request : &deny_input_open);
1354 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1355 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001356}
Ben Lindstrombba81212001-06-25 05:01:22 +00001357static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001358client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001359{
1360 client_init_dispatch_13();
1361 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1362 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1363}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001364static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001365client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001366{
Damien Miller1383bd82000-04-06 12:32:37 +10001367 if (compat20)
1368 client_init_dispatch_20();
1369 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001370 client_init_dispatch_13();
1371 else
1372 client_init_dispatch_15();
1373}