blob: bc50f0bca99a3aea299ebe968231bf149a649d76 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 *
14 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
Damien Miller1383bd82000-04-06 12:32:37 +100037 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000038 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110039 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110059 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61#include "includes.h"
Darren Tucker9a2c4cd2003-09-22 21:16:05 +100062RCSID("$OpenBSD: clientloop.c,v 1.113 2003/09/19 17:43:35 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;
Damien Miller95def091999-11-25 00:26:21 +1100142 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143}
144
Damien Miller95def091999-11-25 00:26:21 +1100145/* Puts stdin terminal in non-blocking mode. */
146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000148enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Miller95def091999-11-25 00:26:21 +1100150 in_non_blocking_mode = 1;
151 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152}
153
Damien Miller5428f641999-11-25 11:54:57 +1100154/*
155 * Signal handler for the window change signal (SIGWINCH). This just sets a
156 * flag indicating that the window has changed.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
Damien Miller95def091999-11-25 00:26:21 +1100160window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161{
Damien Miller95def091999-11-25 00:26:21 +1100162 received_window_change_signal = 1;
163 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164}
165
Damien Miller5428f641999-11-25 11:54:57 +1100166/*
167 * Signal handler for signals that cause the program to terminate. These
168 * signals must be trapped to restore terminal modes.
169 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Ben Lindstrombba81212001-06-25 05:01:22 +0000171static void
Damien Miller95def091999-11-25 00:26:21 +1100172signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000174 received_signal = sig;
175 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176}
177
Damien Miller5428f641999-11-25 11:54:57 +1100178/*
179 * Returns current time in seconds from Jan 1, 1970 with the maximum
180 * available resolution.
181 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000184get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller95def091999-11-25 00:26:21 +1100186 struct timeval tv;
187 gettimeofday(&tv, NULL);
188 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
Damien Miller5428f641999-11-25 11:54:57 +1100191/*
192 * This is called when the interactive is entered. This checks if there is
193 * an EOF coming on stdin. We must check this explicitly, as select() does
194 * not appear to wake up when redirecting from /dev/null.
195 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000198client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 int len;
201 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Miller5428f641999-11-25 11:54:57 +1100203 /*
204 * If standard input is to be "redirected from /dev/null", we simply
205 * mark that we have seen an EOF and send an EOF message to the
206 * server. Otherwise, we try to read a single character; it appears
207 * that for some files, such /dev/null, select() never wakes up for
208 * read for this descriptor, which means that we never get EOF. This
209 * way we will get the EOF if stdin comes from /dev/null or similar.
210 */
Damien Miller95def091999-11-25 00:26:21 +1100211 if (stdin_null_flag) {
212 /* Fake EOF on stdin. */
213 debug("Sending eof.");
214 stdin_eof = 1;
215 packet_start(SSH_CMSG_EOF);
216 packet_send();
217 } else {
Damien Miller95def091999-11-25 00:26:21 +1100218 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller95def091999-11-25 00:26:21 +1100220 /* Check for immediate EOF on stdin. */
221 len = read(fileno(stdin), buf, 1);
222 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100223 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100224 debug("Sending eof.");
225 stdin_eof = 1;
226 packet_start(SSH_CMSG_EOF);
227 packet_send();
228 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Got data. We must store the data in the buffer,
231 * and also process it as an escape character if
232 * appropriate.
233 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000234 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100235 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000236 else
Damien Miller95def091999-11-25 00:26:21 +1100237 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100238 }
Damien Miller95def091999-11-25 00:26:21 +1100239 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Make packets from buffered stdin data, and buffer them for sending to the
246 * connection.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000250client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000252 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Send buffered stdin data to the server. */
255 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100256 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100257 len = buffer_len(&stdin_buffer);
258 /* Keep the packets at reasonable size. */
259 if (len > packet_get_maxsize())
260 len = packet_get_maxsize();
261 packet_start(SSH_CMSG_STDIN_DATA);
262 packet_put_string(buffer_ptr(&stdin_buffer), len);
263 packet_send();
264 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000265 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100266 /* If we have a pending EOF, send it now. */
267 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
268 packet_start(SSH_CMSG_EOF);
269 packet_send();
270 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272}
273
Damien Miller5428f641999-11-25 11:54:57 +1100274/*
275 * Checks if the client window has changed, and sends a packet about it to
276 * the server if so. The actual change is detected elsewhere (by a software
277 * interrupt on Unix); this just checks the flag and sends a message if
278 * appropriate.
279 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Ben Lindstrombba81212001-06-25 05:01:22 +0000281static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000282client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283{
Damien Miller1383bd82000-04-06 12:32:37 +1000284 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller1383bd82000-04-06 12:32:37 +1000286 if (! received_window_change_signal)
287 return;
288 /** XXX race */
289 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller1383bd82000-04-06 12:32:37 +1000291 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
292 return;
293
Damien Millerd3444942000-09-30 14:20:03 +1100294 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000295
296 if (compat20) {
297 channel_request_start(session_ident, "window-change", 0);
298 packet_put_int(ws.ws_col);
299 packet_put_int(ws.ws_row);
300 packet_put_int(ws.ws_xpixel);
301 packet_put_int(ws.ws_ypixel);
302 packet_send();
303 } else {
304 packet_start(SSH_CMSG_WINDOW_SIZE);
305 packet_put_int(ws.ws_row);
306 packet_put_int(ws.ws_col);
307 packet_put_int(ws.ws_xpixel);
308 packet_put_int(ws.ws_ypixel);
309 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
Damien Miller5428f641999-11-25 11:54:57 +1100313/*
314 * Waits until the client can do something (some data becomes available on
315 * one of the file descriptors).
316 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static void
Damien Miller5e953212001-01-30 09:14:00 +1100319client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000320 int *maxfdp, int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Damien Miller5e953212001-01-30 09:14:00 +1100322 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000323 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller1383bd82000-04-06 12:32:37 +1000325 if (!compat20) {
326 /* Read from the connection, unless our buffers are full. */
327 if (buffer_len(&stdout_buffer) < buffer_high &&
328 buffer_len(&stderr_buffer) < buffer_high &&
329 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100330 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000331 /*
332 * Read from stdin, unless we have seen EOF or have very much
333 * buffered data to send to the server.
334 */
335 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100336 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000337
338 /* Select stdout/stderr if have data in buffer. */
339 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100340 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000341 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100342 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000343 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000344 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000345 if (session_closed && !channel_still_open() &&
346 !packet_have_data_to_write()) {
347 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100348 memset(*readsetp, 0, *nallocp);
349 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000350 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000351 } else {
352 FD_SET(connection_in, *readsetp);
353 }
Damien Miller1383bd82000-04-06 12:32:37 +1000354 }
355
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Select server connection if have data to write to the server. */
357 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100358 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100359
Damien Miller5428f641999-11-25 11:54:57 +1100360 /*
361 * Wait for something to happen. This will suspend the process until
362 * some selected descriptor can be read, written, or has some other
363 * event pending. Note: if you want to implement SSH_MSG_IGNORE
364 * messages to fool traffic analysis, this might be the place to do
365 * it: just have a random timeout for the select, and send a random
366 * SSH_MSG_IGNORE packet when the timeout expires.
367 */
Damien Miller95def091999-11-25 00:26:21 +1100368
Damien Miller5e953212001-01-30 09:14:00 +1100369 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100370 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000371
372 /*
373 * We have to clear the select masks, because we return.
374 * We have to return, because the mainloop checks for the flags
375 * set by the signal handlers.
376 */
Damien Miller79faeff2001-11-12 11:06:32 +1100377 memset(*readsetp, 0, *nallocp);
378 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000379
Damien Miller95def091999-11-25 00:26:21 +1100380 if (errno == EINTR)
381 return;
382 /* Note: we might still have data in the buffers. */
383 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
384 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100385 quit_pending = 1;
386 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387}
388
Ben Lindstrombba81212001-06-25 05:01:22 +0000389static void
Damien Millerad833b32000-08-23 10:46:23 +1000390client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391{
Damien Miller95def091999-11-25 00:26:21 +1100392 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
Damien Miller95def091999-11-25 00:26:21 +1100394 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000395 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000396 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000397 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000398 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Damien Miller95def091999-11-25 00:26:21 +1100400 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Damien Miller5428f641999-11-25 11:54:57 +1100402 /*
403 * Free (and clear) the buffer to reduce the amount of data that gets
404 * written to swap.
405 */
Damien Millerad833b32000-08-23 10:46:23 +1000406 buffer_free(bin);
407 buffer_free(bout);
408 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Damien Miller95def091999-11-25 00:26:21 +1100410 /* Save old window size. */
411 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* Send the suspend signal to the program itself. */
414 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Check if the window size has changed. */
417 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
418 (oldws.ws_row != newws.ws_row ||
Damien Miller9f0f5c62001-12-21 14:45:46 +1100419 oldws.ws_col != newws.ws_col ||
420 oldws.ws_xpixel != newws.ws_xpixel ||
421 oldws.ws_ypixel != newws.ws_ypixel))
Damien Miller95def091999-11-25 00:26:21 +1100422 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000425 buffer_init(bin);
426 buffer_init(bout);
427 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Damien Miller95def091999-11-25 00:26:21 +1100429 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}
431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
Damien Miller1383bd82000-04-06 12:32:37 +1000433client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434{
Damien Miller1383bd82000-04-06 12:32:37 +1000435 int len;
436 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller5428f641999-11-25 11:54:57 +1100438 /*
439 * Read input from the server, and add any such data to the buffer of
440 * the packet subsystem.
441 */
Damien Miller95def091999-11-25 00:26:21 +1100442 if (FD_ISSET(connection_in, readset)) {
443 /* Read as much as possible. */
444 len = read(connection_in, buf, sizeof(buf));
445 if (len == 0) {
446 /* Received EOF. The remote host has closed the connection. */
447 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
448 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450 quit_pending = 1;
451 return;
Damien Miller95def091999-11-25 00:26:21 +1100452 }
Damien Miller5428f641999-11-25 11:54:57 +1100453 /*
454 * There is a kernel bug on Solaris that causes select to
455 * sometimes wake up even though there is no data available.
456 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000457 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100458 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller95def091999-11-25 00:26:21 +1100460 if (len < 0) {
461 /* An error has encountered. Perhaps there is a network problem. */
462 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
463 host, strerror(errno));
464 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100465 quit_pending = 1;
466 return;
467 }
468 packet_process_incoming(buf, len);
469 }
Damien Miller1383bd82000-04-06 12:32:37 +1000470}
471
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000472static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000473process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000474{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000475 void (*handler)(int);
476 char *s, *cmd;
477 u_short fwd_port, fwd_host_port;
478 char buf[1024], sfwd_port[6], sfwd_host_port[6];
479 int local = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000480
481 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000482 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000483 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000484 if (s == NULL)
485 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000486 while (*s && isspace(*s))
487 s++;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000488 if (*s == 0)
489 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000490 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
Damien Miller996acd22003-04-09 20:59:48 +1000491 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000492 goto out;
493 }
494 if (s[1] == 'L')
495 local = 1;
496 if (!local && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000497 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000498 goto out;
499 }
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000500 s += 2;
501 while (*s && isspace(*s))
502 s++;
503
504 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
505 sfwd_port, buf, sfwd_host_port) != 3 &&
506 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]",
507 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Miller996acd22003-04-09 20:59:48 +1000508 logit("Bad forwarding specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000509 goto out;
510 }
511 if ((fwd_port = a2port(sfwd_port)) == 0 ||
512 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000513 logit("Bad forwarding port(s).");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000514 goto out;
515 }
516 if (local) {
Ben Lindstrom681d9322002-03-22 03:53:00 +0000517 if (channel_setup_local_fwd_listener(fwd_port, buf,
518 fwd_host_port, options.gateway_ports) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000519 logit("Port forwarding failed.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000520 goto out;
521 }
522 } else
523 channel_request_remote_forwarding(fwd_port, buf,
524 fwd_host_port);
Damien Miller996acd22003-04-09 20:59:48 +1000525 logit("Forwarding port.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000526out:
527 signal(SIGINT, handler);
528 enter_raw_mode();
529 if (cmd)
530 xfree(cmd);
531}
532
Damien Millerad833b32000-08-23 10:46:23 +1000533/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +0000534static int
Damien Millerad833b32000-08-23 10:46:23 +1000535process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
536{
537 char string[1024];
538 pid_t pid;
539 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540 u_int i;
541 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000542 char *s;
543
544 for (i = 0; i < len; i++) {
545 /* Get one character at a time. */
546 ch = buf[i];
547
548 if (escape_pending) {
549 /* We have previously seen an escape character. */
550 /* Clear the flag now. */
551 escape_pending = 0;
552
553 /* Process the escaped character. */
554 switch (ch) {
555 case '.':
556 /* Terminate the connection. */
557 snprintf(string, sizeof string, "%c.\r\n", escape_char);
558 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000559
560 quit_pending = 1;
561 return -1;
562
563 case 'Z' - 64:
564 /* Suspend the program. */
565 /* Print a message to that effect to the user. */
566 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
567 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000568
569 /* Restore terminal modes and suspend. */
570 client_suspend_self(bin, bout, berr);
571
572 /* We have been continued. */
573 continue;
574
Damien Miller54c45982003-05-15 10:20:13 +1000575 case 'B':
576 if (compat20) {
577 snprintf(string, sizeof string,
578 "%cB\r\n", escape_char);
579 buffer_append(berr, string,
580 strlen(string));
581 channel_request_start(session_ident,
582 "break", 0);
583 packet_put_int(1000);
584 packet_send();
585 }
586 continue;
587
Ben Lindstromf28f6342001-04-04 02:03:04 +0000588 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000589 if (compat20) {
590 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +1000591 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000592 else
593 need_rekeying = 1;
594 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000595 continue;
596
Damien Millerad833b32000-08-23 10:46:23 +1000597 case '&':
Damien Millerad833b32000-08-23 10:46:23 +1000598 /*
599 * Detach the program (continue to serve connections,
600 * but put in background and no more new connections).
601 */
Damien Miller96507ef2001-11-12 10:52:25 +1100602 /* Restore tty modes. */
603 leave_raw_mode();
604
605 /* Stop listening for new connections. */
606 channel_stop_listening();
607
608 snprintf(string, sizeof string,
609 "%c& [backgrounded]\n", escape_char);
610 buffer_append(berr, string, strlen(string));
611
612 /* Fork into background. */
613 pid = fork();
614 if (pid < 0) {
615 error("fork: %.100s", strerror(errno));
616 continue;
617 }
618 if (pid != 0) { /* This is the parent. */
619 /* The parent just exits. */
620 exit(0);
621 }
622 /* The child continues serving connections. */
623 if (compat20) {
624 buffer_append(bin, "\004", 1);
625 /* fake EOF on stdin */
626 return -1;
627 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +1000628 /*
629 * Sending SSH_CMSG_EOF alone does not always appear
630 * to be enough. So we try to send an EOF character
631 * first.
632 */
633 packet_start(SSH_CMSG_STDIN_DATA);
634 packet_put_string("\004", 1);
635 packet_send();
636 /* Close stdin. */
637 stdin_eof = 1;
638 if (buffer_len(bin) == 0) {
639 packet_start(SSH_CMSG_EOF);
640 packet_send();
641 }
642 }
Damien Miller96507ef2001-11-12 10:52:25 +1100643 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000644
645 case '?':
646 snprintf(string, sizeof string,
647"%c?\r\n\
648Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000649%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +1000650%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +1000651%cC - open a command line\r\n\
652%cR - Request rekey (SSH protocol 2 only)\r\n\
653%c^Z - suspend ssh\r\n\
654%c# - list forwarded connections\r\n\
655%c& - background ssh (when waiting for connections to terminate)\r\n\
656%c? - this message\r\n\
657%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000658(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +1000659 escape_char, escape_char, escape_char, escape_char,
660 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +1000661 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000662 buffer_append(berr, string, strlen(string));
663 continue;
664
665 case '#':
666 snprintf(string, sizeof string, "%c#\r\n", escape_char);
667 buffer_append(berr, string, strlen(string));
668 s = channel_open_message();
669 buffer_append(berr, s, strlen(s));
670 xfree(s);
671 continue;
672
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000673 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +0000674 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000675 continue;
676
Damien Millerad833b32000-08-23 10:46:23 +1000677 default:
678 if (ch != escape_char) {
679 buffer_put_char(bin, escape_char);
680 bytes++;
681 }
682 /* Escaped characters fall through here */
683 break;
684 }
685 } else {
686 /*
687 * The previous character was not an escape char. Check if this
688 * is an escape.
689 */
690 if (last_was_cr && ch == escape_char) {
691 /* It is. Set the flag and continue to next character. */
692 escape_pending = 1;
693 continue;
694 }
695 }
696
697 /*
698 * Normal character. Record whether it was a newline,
699 * and append it to the buffer.
700 */
701 last_was_cr = (ch == '\r' || ch == '\n');
702 buffer_put_char(bin, ch);
703 bytes++;
704 }
705 return bytes;
706}
707
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static void
Damien Miller1383bd82000-04-06 12:32:37 +1000709client_process_input(fd_set * readset)
710{
Damien Miller166fca82000-04-20 07:42:21 +1000711 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000712 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000713
Damien Miller95def091999-11-25 00:26:21 +1100714 /* Read input from stdin. */
715 if (FD_ISSET(fileno(stdin), readset)) {
716 /* Read as much as possible. */
717 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000718 if (len < 0 && (errno == EAGAIN || errno == EINTR))
719 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100720 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100721 /*
722 * Received EOF or error. They are treated
723 * similarly, except that an error message is printed
724 * if it was an error condition.
725 */
Damien Miller95def091999-11-25 00:26:21 +1100726 if (len < 0) {
727 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
728 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100729 }
730 /* Mark that we have seen EOF. */
731 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100732 /*
733 * Send an EOF message to the server unless there is
734 * data in the buffer. If there is data in the
735 * buffer, no message will be sent now. Code
736 * elsewhere will send the EOF when the buffer
737 * becomes empty if stdin_eof is set.
738 */
Damien Miller95def091999-11-25 00:26:21 +1100739 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 packet_start(SSH_CMSG_EOF);
741 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100742 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000743 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +1100744 /*
745 * Normal successful read, and no escape character.
746 * Just append the data to buffer.
747 */
Damien Miller95def091999-11-25 00:26:21 +1100748 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100749 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100750 /*
751 * Normal, successful read. But we have an escape character
752 * and have to process the characters one by one.
753 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000754 if (process_escapes(&stdin_buffer, &stdout_buffer,
755 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000756 return;
Damien Miller95def091999-11-25 00:26:21 +1100757 }
758 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759}
760
Ben Lindstrombba81212001-06-25 05:01:22 +0000761static void
Damien Miller95def091999-11-25 00:26:21 +1100762client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763{
Damien Miller95def091999-11-25 00:26:21 +1100764 int len;
765 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766
Damien Miller95def091999-11-25 00:26:21 +1100767 /* Write buffered output to stdout. */
768 if (FD_ISSET(fileno(stdout), writeset)) {
769 /* Write as much data as possible. */
770 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100771 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100772 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000773 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100774 len = 0;
775 else {
Damien Miller5428f641999-11-25 11:54:57 +1100776 /*
777 * An error or EOF was encountered. Put an
778 * error message to stderr buffer.
779 */
Damien Miller95def091999-11-25 00:26:21 +1100780 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
781 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100782 quit_pending = 1;
783 return;
784 }
785 }
786 /* Consume printed data from the buffer. */
787 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000788 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789 }
Damien Miller95def091999-11-25 00:26:21 +1100790 /* Write buffered output to stderr. */
791 if (FD_ISSET(fileno(stderr), writeset)) {
792 /* Write as much data as possible. */
793 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100794 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100795 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000796 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100797 len = 0;
798 else {
Damien Miller5428f641999-11-25 11:54:57 +1100799 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100800 quit_pending = 1;
801 return;
802 }
803 }
804 /* Consume printed characters from the buffer. */
805 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000806 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100807 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808}
809
Damien Miller5428f641999-11-25 11:54:57 +1100810/*
Damien Millerb38eff82000-04-01 11:09:21 +1000811 * Get packets from the connection input buffer, and process them as long as
812 * there are packets available.
813 *
814 * Any unknown packets received during the actual
815 * session cause the session to terminate. This is
816 * intended to make debugging easier since no
817 * confirmations are sent. Any compatible protocol
818 * extensions must be negotiated during the
819 * preparatory phase.
820 */
821
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000823client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000824{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000825 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000826}
827
Damien Millerad833b32000-08-23 10:46:23 +1000828/* scan buf[] for '~' before sending data to the peer */
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static int
Damien Millerad833b32000-08-23 10:46:23 +1000831simple_escape_filter(Channel *c, char *buf, int len)
832{
833 /* XXX we assume c->extended is writeable */
834 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
835}
836
Ben Lindstrombba81212001-06-25 05:01:22 +0000837static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000838client_channel_closed(int id, void *arg)
839{
840 if (id != session_ident)
841 error("client_channel_closed: id %d != session_ident %d",
842 id, session_ident);
Damien Miller3ec27592001-10-12 11:35:04 +1000843 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000844 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +1000845 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000846}
847
Damien Millerb38eff82000-04-01 11:09:21 +1000848/*
Damien Miller5428f641999-11-25 11:54:57 +1100849 * Implements the interactive session with the server. This is called after
850 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000851 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
852 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +1100853 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854
Damien Miller4af51302000-04-16 11:18:38 +1000855int
Damien Millerad833b32000-08-23 10:46:23 +1000856client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857{
Damien Miller5e953212001-01-30 09:14:00 +1100858 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100859 double start_time, total_time;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000860 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +1100861 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862
Damien Miller95def091999-11-25 00:26:21 +1100863 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864
Damien Miller95def091999-11-25 00:26:21 +1100865 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Damien Miller95def091999-11-25 00:26:21 +1100867 /* Initialize variables. */
868 escape_pending = 0;
869 last_was_cr = 1;
870 exit_status = -1;
871 stdin_eof = 0;
872 buffer_high = 64 * 1024;
873 connection_in = packet_get_connection_in();
874 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100875 max_fd = MAX(connection_in, connection_out);
876
877 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000878 /* enable nonblocking unless tty */
879 if (!isatty(fileno(stdin)))
880 set_nonblock(fileno(stdin));
881 if (!isatty(fileno(stdout)))
882 set_nonblock(fileno(stdout));
883 if (!isatty(fileno(stderr)))
884 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100885 max_fd = MAX(max_fd, fileno(stdin));
886 max_fd = MAX(max_fd, fileno(stdout));
887 max_fd = MAX(max_fd, fileno(stderr));
888 }
Damien Miller95def091999-11-25 00:26:21 +1100889 stdin_bytes = 0;
890 stdout_bytes = 0;
891 stderr_bytes = 0;
892 quit_pending = 0;
893 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
Damien Miller95def091999-11-25 00:26:21 +1100895 /* Initialize buffers. */
896 buffer_init(&stdin_buffer);
897 buffer_init(&stdout_buffer);
898 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Millerb38eff82000-04-01 11:09:21 +1000900 client_init_dispatch();
901
Ben Lindstromf49dbff2002-12-23 02:01:55 +0000902 /*
903 * Set signal handlers, (e.g. to restore non-blocking mode)
904 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
905 */
906 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
907 signal(SIGINT, signal_handler);
908 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
909 signal(SIGQUIT, signal_handler);
910 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
911 signal(SIGTERM, signal_handler);
Damien Miller95def091999-11-25 00:26:21 +1100912 if (have_pty)
913 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 if (have_pty)
916 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Ben Lindstrom5b828322001-02-09 01:34:36 +0000918 if (compat20) {
919 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000920 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +0000921 channel_register_filter(session_ident,
922 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000923 if (session_ident != -1)
924 channel_register_cleanup(session_ident,
925 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000926 } else {
927 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000928 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000929 }
Damien Millerad833b32000-08-23 10:46:23 +1000930
Damien Miller95def091999-11-25 00:26:21 +1100931 /* Main loop of the client for the interactive session mode. */
932 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller5428f641999-11-25 11:54:57 +1100934 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100935 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000937 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000938 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000939
940 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000941
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000942 if (rekeying) {
943 debug("rekeying in progress");
944 } else {
945 /*
946 * Make packets of buffered stdin data, and buffer
947 * them for sending to the server.
948 */
949 if (!compat20)
950 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000952 /*
953 * Make packets from buffered channel data, and
954 * enqueue them for sending to the server.
955 */
956 if (packet_not_very_much_data_to_write())
957 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000959 /*
960 * Check if the window size has changed, and buffer a
961 * message about it to the server if so.
962 */
963 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000965 if (quit_pending)
966 break;
967 }
Damien Miller5428f641999-11-25 11:54:57 +1100968 /*
969 * Wait until we have something to do (something becomes
970 * available on one of the descriptors).
971 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000972 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000973 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000974 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller95def091999-11-25 00:26:21 +1100976 if (quit_pending)
977 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000979 /* Do channel operations unless rekeying in progress. */
980 if (!rekeying) {
981 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +1000982 if (need_rekeying || packet_need_rekeying()) {
983 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000984 xxx_kex->done = 0;
985 kex_send_kexinit(xxx_kex);
986 need_rekeying = 0;
987 }
988 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989
Damien Miller1383bd82000-04-06 12:32:37 +1000990 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +1100991 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992
Damien Miller1383bd82000-04-06 12:32:37 +1000993 if (quit_pending)
994 break;
995
996 if (!compat20) {
997 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +1100998 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +1000999 /*
1000 * Process output to stdout and stderr. Output to
1001 * the connection is processed elsewhere (above).
1002 */
Damien Miller5e953212001-01-30 09:14:00 +11001003 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001004 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Damien Miller5428f641999-11-25 11:54:57 +11001006 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001007 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001008 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009 }
Damien Miller5e953212001-01-30 09:14:00 +11001010 if (readset)
1011 xfree(readset);
1012 if (writeset)
1013 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Miller95def091999-11-25 00:26:21 +11001015 /* Terminate the session. */
1016
1017 /* Stop watching for window change. */
1018 if (have_pty)
1019 signal(SIGWINCH, SIG_DFL);
1020
Ben Lindstrom601e4362001-06-21 03:19:23 +00001021 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001022
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001023 if (have_pty)
1024 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001025
1026 /* restore blocking io */
1027 if (!isatty(fileno(stdin)))
1028 unset_nonblock(fileno(stdin));
1029 if (!isatty(fileno(stdout)))
1030 unset_nonblock(fileno(stdout));
1031 if (!isatty(fileno(stderr)))
1032 unset_nonblock(fileno(stderr));
1033
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001034 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001035 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001036
1037 /*
1038 * In interactive mode (with pseudo tty) display a message indicating
1039 * that the connection has been closed.
1040 */
1041 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1042 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1043 buffer_append(&stderr_buffer, buf, strlen(buf));
1044 }
1045
Damien Miller95def091999-11-25 00:26:21 +11001046 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001047 while (buffer_len(&stdout_buffer) > 0) {
1048 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001049 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001050 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001051 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001052 break;
1053 }
Damien Miller95def091999-11-25 00:26:21 +11001054 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001055 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001056 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057
Damien Miller95def091999-11-25 00:26:21 +11001058 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001059 while (buffer_len(&stderr_buffer) > 0) {
1060 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001061 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001062 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001063 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001064 break;
1065 }
Damien Miller95def091999-11-25 00:26:21 +11001066 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001067 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001068 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller95def091999-11-25 00:26:21 +11001070 /* Clear and free any buffers. */
1071 memset(buf, 0, sizeof(buf));
1072 buffer_free(&stdin_buffer);
1073 buffer_free(&stdout_buffer);
1074 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075
Damien Miller95def091999-11-25 00:26:21 +11001076 /* Report bytes transferred, and transfer rates. */
1077 total_time = get_current_time() - start_time;
1078 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001079 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001080 if (total_time > 0)
1081 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001082 stdin_bytes / total_time, stdout_bytes / total_time,
1083 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001084
1085 /* Return the exit status of the program. */
1086 debug("Exit status %d", exit_status);
1087 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088}
Damien Millerb38eff82000-04-01 11:09:21 +10001089
1090/*********/
1091
Ben Lindstrombba81212001-06-25 05:01:22 +00001092static void
Damien Miller630d6f42002-01-22 23:17:30 +11001093client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001094{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001095 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001096 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001097 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001098 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001099 memset(data, 0, data_len);
1100 xfree(data);
1101}
Ben Lindstrombba81212001-06-25 05:01:22 +00001102static void
Damien Miller630d6f42002-01-22 23:17:30 +11001103client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001104{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001105 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001106 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001107 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001108 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001109 memset(data, 0, data_len);
1110 xfree(data);
1111}
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static void
Damien Miller630d6f42002-01-22 23:17:30 +11001113client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001114{
Damien Millerb38eff82000-04-01 11:09:21 +10001115 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001116 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001117 /* Acknowledge the exit. */
1118 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1119 packet_send();
1120 /*
1121 * Must wait for packet to be sent since we are
1122 * exiting the loop.
1123 */
1124 packet_write_wait();
1125 /* Flag that we want to exit. */
1126 quit_pending = 1;
1127}
1128
Ben Lindstrombba81212001-06-25 05:01:22 +00001129static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001130client_request_forwarded_tcpip(const char *request_type, int rchan)
1131{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001132 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 char *listen_address, *originator_address;
1134 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001135 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001136
1137 /* Get rest of the packet */
1138 listen_address = packet_get_string(NULL);
1139 listen_port = packet_get_int();
1140 originator_address = packet_get_string(NULL);
1141 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001142 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001143
1144 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1145 listen_address, listen_port, originator_address, originator_port);
1146
Ben Lindstrom173e6462001-07-04 05:15:15 +00001147 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 if (sock < 0) {
1149 xfree(originator_address);
1150 xfree(listen_address);
1151 return NULL;
1152 }
1153 c = channel_new("forwarded-tcpip",
1154 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1155 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001156 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001157 xfree(originator_address);
1158 xfree(listen_address);
1159 return c;
1160}
1161
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001162static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001163client_request_x11(const char *request_type, int rchan)
1164{
1165 Channel *c = NULL;
1166 char *originator;
1167 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001168 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001169
1170 if (!options.forward_x11) {
1171 error("Warning: ssh server tried X11 forwarding.");
1172 error("Warning: this is probably a break in attempt by a malicious server.");
1173 return NULL;
1174 }
1175 originator = packet_get_string(NULL);
1176 if (datafellows & SSH_BUG_X11FWD) {
1177 debug2("buggy server: x11 request w/o originator_port");
1178 originator_port = 0;
1179 } else {
1180 originator_port = packet_get_int();
1181 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001182 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001183 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001184 debug("client_request_x11: request from %s %d", originator,
1185 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001186 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001187 sock = x11_connect_display();
1188 if (sock < 0)
1189 return NULL;
1190 c = channel_new("x11",
1191 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001192 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001193 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001194 return c;
1195}
1196
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001197static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001198client_request_agent(const char *request_type, int rchan)
1199{
1200 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001201 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001202
1203 if (!options.forward_agent) {
1204 error("Warning: ssh server tried agent forwarding.");
1205 error("Warning: this is probably a break in attempt by a malicious server.");
1206 return NULL;
1207 }
1208 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001209 if (sock < 0)
1210 return NULL;
1211 c = channel_new("authentication agent connection",
1212 SSH_CHANNEL_OPEN, sock, sock, -1,
1213 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001214 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001215 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216 return c;
1217}
1218
Damien Millerbd483e72000-04-30 10:00:53 +10001219/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001220static void
Damien Miller630d6f42002-01-22 23:17:30 +11001221client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001222{
1223 Channel *c = NULL;
1224 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001225 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001226 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001227
1228 ctype = packet_get_string(&len);
1229 rchan = packet_get_int();
1230 rwindow = packet_get_int();
1231 rmaxpack = packet_get_int();
1232
Damien Millere247cc42000-05-07 12:03:14 +10001233 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001234 ctype, rchan, rwindow, rmaxpack);
1235
Damien Miller0bc1bd82000-11-13 22:57:25 +11001236 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1237 c = client_request_forwarded_tcpip(ctype, rchan);
1238 } else if (strcmp(ctype, "x11") == 0) {
1239 c = client_request_x11(ctype, rchan);
1240 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1241 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001242 }
1243/* XXX duplicate : */
1244 if (c != NULL) {
1245 debug("confirm %s", ctype);
1246 c->remote_id = rchan;
1247 c->remote_window = rwindow;
1248 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001249 if (c->type != SSH_CHANNEL_CONNECTING) {
1250 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1251 packet_put_int(c->remote_id);
1252 packet_put_int(c->self);
1253 packet_put_int(c->local_window);
1254 packet_put_int(c->local_maxpacket);
1255 packet_send();
1256 }
Damien Millerbd483e72000-04-30 10:00:53 +10001257 } else {
1258 debug("failure %s", ctype);
1259 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1260 packet_put_int(rchan);
1261 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001262 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001263 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001264 packet_put_cstring("");
1265 }
Damien Millerbd483e72000-04-30 10:00:53 +10001266 packet_send();
1267 }
1268 xfree(ctype);
1269}
Ben Lindstrombba81212001-06-25 05:01:22 +00001270static void
Damien Miller630d6f42002-01-22 23:17:30 +11001271client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001272{
1273 Channel *c = NULL;
1274 int id, reply, success = 0;
1275 char *rtype;
1276
1277 id = packet_get_int();
1278 rtype = packet_get_string(NULL);
1279 reply = packet_get_char();
1280
1281 debug("client_input_channel_req: channel %d rtype %s reply %d",
1282 id, rtype, reply);
1283
1284 if (session_ident == -1) {
1285 error("client_input_channel_req: no channel %d", session_ident);
1286 } else if (id != session_ident) {
1287 error("client_input_channel_req: channel %d: wrong channel: %d",
1288 session_ident, id);
1289 }
1290 c = channel_lookup(id);
1291 if (c == NULL) {
1292 error("client_input_channel_req: channel %d: unknown channel", id);
1293 } else if (strcmp(rtype, "exit-status") == 0) {
1294 success = 1;
1295 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001296 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001297 }
1298 if (reply) {
1299 packet_start(success ?
1300 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1301 packet_put_int(c->remote_id);
1302 packet_send();
1303 }
1304 xfree(rtype);
1305}
Damien Millerc3fa4072002-01-22 23:21:58 +11001306static void
1307client_input_global_request(int type, u_int32_t seq, void *ctxt)
1308{
1309 char *rtype;
1310 int want_reply;
1311 int success = 0;
1312
1313 rtype = packet_get_string(NULL);
1314 want_reply = packet_get_char();
1315 debug("client_input_global_request: rtype %s want_reply %d", rtype, want_reply);
1316 if (want_reply) {
1317 packet_start(success ?
1318 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1319 packet_send();
1320 packet_write_wait();
1321 }
1322 xfree(rtype);
1323}
Damien Millerbd483e72000-04-30 10:00:53 +10001324
Ben Lindstrombba81212001-06-25 05:01:22 +00001325static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001326client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001327{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001328 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001329
Damien Miller1383bd82000-04-06 12:32:37 +10001330 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1331 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1332 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1333 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001334 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001335 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1336 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001337 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001338 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001339 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001340
1341 /* rekeying */
1342 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001343
1344 /* global request reply messages */
1345 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1346 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001347}
Ben Lindstrombba81212001-06-25 05:01:22 +00001348static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001349client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001350{
1351 dispatch_init(NULL);
1352 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1353 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1354 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001355 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1356 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1357 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001358 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1359 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1360 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001361
1362 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1363 &auth_input_open_request : &deny_input_open);
1364 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1365 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001366}
Ben Lindstrombba81212001-06-25 05:01:22 +00001367static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001368client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001369{
1370 client_init_dispatch_13();
1371 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1372 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1373}
Ben Lindstromdb47f382001-07-04 05:10:27 +00001374static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001375client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001376{
Damien Miller1383bd82000-04-06 12:32:37 +10001377 if (compat20)
1378 client_init_dispatch_20();
1379 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001380 client_init_dispatch_13();
1381 else
1382 client_init_dispatch_15();
1383}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001384
1385/* client specific fatal cleanup */
1386void
1387fatal(const char *fmt,...)
1388{
1389 va_list args;
1390
1391 va_start(args, fmt);
1392 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
1393 va_end(args);
1394 leave_raw_mode();
1395 leave_non_blocking();
1396 _exit(255);
1397}