blob: 2709ce356f0ebf603df98428964e4a346fc5b241 [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.
Damien Millere4340be2000-09-16 13:29:08 +110038 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
39 *
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 Lindstrom4c8cff12001-04-17 18:09:42 +000062RCSID("$OpenBSD: clientloop.c,v 1.64 2001/04/17 09:52:48 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"
71#include "channels.h"
72#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"
Damien Miller69b69aa2000-10-28 14:19:58 +110084
85/* import options */
86extern Options options;
87
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088/* Flag indicating that stdin should be redirected from /dev/null. */
89extern int stdin_null_flag;
90
Damien Miller5428f641999-11-25 11:54:57 +110091/*
92 * Name of the host we are connecting to. This is the name given on the
93 * command line, or the HostName specified for the user-supplied name in a
94 * configuration file.
95 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096extern char *host;
97
Damien Miller5428f641999-11-25 11:54:57 +110098/*
99 * Flag to indicate that we have received a window change signal which has
100 * not yet been processed. This will cause a message indicating the new
101 * window size to be sent to the server a little later. This is volatile
102 * because this is updated in a signal handler.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104static volatile int received_window_change_signal = 0;
105
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
107static int in_non_blocking_mode = 0;
108
109/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000110static int quit_pending; /* Set to non-zero to quit the client loop. */
111static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100112static int escape_pending; /* Last character was the escape character */
113static int last_was_cr; /* Last character was a newline. */
114static int exit_status; /* Used to store the exit status of the command. */
115static int stdin_eof; /* EOF has been encountered on standard error. */
116static Buffer stdin_buffer; /* Buffer for stdin data. */
117static Buffer stdout_buffer; /* Buffer for stdout data. */
118static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000119static u_long stdin_bytes, stdout_bytes, stderr_bytes;
120static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100121static int connection_in; /* Connection to server (input). */
122static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000123static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000124static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller1383bd82000-04-06 12:32:37 +1000126void client_init_dispatch(void);
127int session_ident = -1;
128
Ben Lindstromf28f6342001-04-04 02:03:04 +0000129/*XXX*/
130extern Kex *xxx_kex;
131
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132/* Restores stdin to blocking mode. */
133
Damien Miller4af51302000-04-16 11:18:38 +1000134void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000135leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136{
Damien Miller95def091999-11-25 00:26:21 +1100137 if (in_non_blocking_mode) {
138 (void) fcntl(fileno(stdin), F_SETFL, 0);
139 in_non_blocking_mode = 0;
140 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
141 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller95def091999-11-25 00:26:21 +1100144/* Puts stdin terminal in non-blocking mode. */
145
Damien Miller4af51302000-04-16 11:18:38 +1000146void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000147enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148{
Damien Miller95def091999-11-25 00:26:21 +1100149 in_non_blocking_mode = 1;
150 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
151 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
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
Damien Miller4af51302000-04-16 11:18:38 +1000159void
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
Damien Miller4af51302000-04-16 11:18:38 +1000171void
Damien Miller95def091999-11-25 00:26:21 +1100172signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000174 if (in_raw_mode())
Damien Miller95def091999-11-25 00:26:21 +1100175 leave_raw_mode();
176 if (in_non_blocking_mode)
177 leave_non_blocking();
178 channel_stop_listening();
179 packet_close();
180 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Damien Miller5428f641999-11-25 11:54:57 +1100183/*
184 * Returns current time in seconds from Jan 1, 1970 with the maximum
185 * available resolution.
186 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Miller4af51302000-04-16 11:18:38 +1000188double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000189get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190{
Damien Miller95def091999-11-25 00:26:21 +1100191 struct timeval tv;
192 gettimeofday(&tv, NULL);
193 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194}
195
Damien Miller5428f641999-11-25 11:54:57 +1100196/*
197 * This is called when the interactive is entered. This checks if there is
198 * an EOF coming on stdin. We must check this explicitly, as select() does
199 * not appear to wake up when redirecting from /dev/null.
200 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller4af51302000-04-16 11:18:38 +1000202void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000203client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 int len;
206 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207
Damien Miller5428f641999-11-25 11:54:57 +1100208 /*
209 * If standard input is to be "redirected from /dev/null", we simply
210 * mark that we have seen an EOF and send an EOF message to the
211 * server. Otherwise, we try to read a single character; it appears
212 * that for some files, such /dev/null, select() never wakes up for
213 * read for this descriptor, which means that we never get EOF. This
214 * way we will get the EOF if stdin comes from /dev/null or similar.
215 */
Damien Miller95def091999-11-25 00:26:21 +1100216 if (stdin_null_flag) {
217 /* Fake EOF on stdin. */
218 debug("Sending eof.");
219 stdin_eof = 1;
220 packet_start(SSH_CMSG_EOF);
221 packet_send();
222 } else {
Damien Miller95def091999-11-25 00:26:21 +1100223 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225 /* Check for immediate EOF on stdin. */
226 len = read(fileno(stdin), buf, 1);
227 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100228 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100229 debug("Sending eof.");
230 stdin_eof = 1;
231 packet_start(SSH_CMSG_EOF);
232 packet_send();
233 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100234 /*
235 * Got data. We must store the data in the buffer,
236 * and also process it as an escape character if
237 * appropriate.
238 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000239 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100240 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000241 else
Damien Miller95def091999-11-25 00:26:21 +1100242 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100243 }
Damien Miller95def091999-11-25 00:26:21 +1100244 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller5428f641999-11-25 11:54:57 +1100249/*
250 * Make packets from buffered stdin data, and buffer them for sending to the
251 * connection.
252 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller4af51302000-04-16 11:18:38 +1000254void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000255client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000257 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 /* Send buffered stdin data to the server. */
260 while (buffer_len(&stdin_buffer) > 0 &&
261 packet_not_very_much_data_to_write()) {
262 len = buffer_len(&stdin_buffer);
263 /* Keep the packets at reasonable size. */
264 if (len > packet_get_maxsize())
265 len = packet_get_maxsize();
266 packet_start(SSH_CMSG_STDIN_DATA);
267 packet_put_string(buffer_ptr(&stdin_buffer), len);
268 packet_send();
269 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000270 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100271 /* If we have a pending EOF, send it now. */
272 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
273 packet_start(SSH_CMSG_EOF);
274 packet_send();
275 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277}
278
Damien Miller5428f641999-11-25 11:54:57 +1100279/*
280 * Checks if the client window has changed, and sends a packet about it to
281 * the server if so. The actual change is detected elsewhere (by a software
282 * interrupt on Unix); this just checks the flag and sends a message if
283 * appropriate.
284 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller4af51302000-04-16 11:18:38 +1000286void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000287client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Damien Miller1383bd82000-04-06 12:32:37 +1000289 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller1383bd82000-04-06 12:32:37 +1000291 if (! received_window_change_signal)
292 return;
293 /** XXX race */
294 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
Damien Miller1383bd82000-04-06 12:32:37 +1000296 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
297 return;
298
Damien Millerd3444942000-09-30 14:20:03 +1100299 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000300
301 if (compat20) {
302 channel_request_start(session_ident, "window-change", 0);
303 packet_put_int(ws.ws_col);
304 packet_put_int(ws.ws_row);
305 packet_put_int(ws.ws_xpixel);
306 packet_put_int(ws.ws_ypixel);
307 packet_send();
308 } else {
309 packet_start(SSH_CMSG_WINDOW_SIZE);
310 packet_put_int(ws.ws_row);
311 packet_put_int(ws.ws_col);
312 packet_put_int(ws.ws_xpixel);
313 packet_put_int(ws.ws_ypixel);
314 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316}
317
Damien Miller5428f641999-11-25 11:54:57 +1100318/*
319 * Waits until the client can do something (some data becomes available on
320 * one of the file descriptors).
321 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller4af51302000-04-16 11:18:38 +1000323void
Damien Miller5e953212001-01-30 09:14:00 +1100324client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000325 int *maxfdp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Damien Miller5e953212001-01-30 09:14:00 +1100327 /* Add any selections by the channel mechanism. */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000328 channel_prepare_select(readsetp, writesetp, maxfdp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329
Damien Miller1383bd82000-04-06 12:32:37 +1000330 if (!compat20) {
331 /* Read from the connection, unless our buffers are full. */
332 if (buffer_len(&stdout_buffer) < buffer_high &&
333 buffer_len(&stderr_buffer) < buffer_high &&
334 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100335 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000336 /*
337 * Read from stdin, unless we have seen EOF or have very much
338 * buffered data to send to the server.
339 */
340 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100341 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000342
343 /* Select stdout/stderr if have data in buffer. */
344 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100345 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000346 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100347 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000348 } else {
Damien Miller5e953212001-01-30 09:14:00 +1100349 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000350 }
351
Damien Miller95def091999-11-25 00:26:21 +1100352 /* Select server connection if have data to write to the server. */
353 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100354 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100355
Damien Miller5428f641999-11-25 11:54:57 +1100356 /*
357 * Wait for something to happen. This will suspend the process until
358 * some selected descriptor can be read, written, or has some other
359 * event pending. Note: if you want to implement SSH_MSG_IGNORE
360 * messages to fool traffic analysis, this might be the place to do
361 * it: just have a random timeout for the select, and send a random
362 * SSH_MSG_IGNORE packet when the timeout expires.
363 */
Damien Miller95def091999-11-25 00:26:21 +1100364
Damien Miller5e953212001-01-30 09:14:00 +1100365 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100366 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000367
368 /*
369 * We have to clear the select masks, because we return.
370 * We have to return, because the mainloop checks for the flags
371 * set by the signal handlers.
372 */
373 memset(*readsetp, 0, *maxfdp);
374 memset(*writesetp, 0, *maxfdp);
375
Damien Miller95def091999-11-25 00:26:21 +1100376 if (errno == EINTR)
377 return;
378 /* Note: we might still have data in the buffers. */
379 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
380 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100381 quit_pending = 1;
382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383}
384
Damien Miller4af51302000-04-16 11:18:38 +1000385void
Damien Millerad833b32000-08-23 10:46:23 +1000386client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387{
Damien Miller95def091999-11-25 00:26:21 +1100388 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Damien Miller95def091999-11-25 00:26:21 +1100390 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000391 if (buffer_len(bout) > 0)
392 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
393 if (buffer_len(berr) > 0)
394 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397
Damien Miller5428f641999-11-25 11:54:57 +1100398 /*
399 * Free (and clear) the buffer to reduce the amount of data that gets
400 * written to swap.
401 */
Damien Millerad833b32000-08-23 10:46:23 +1000402 buffer_free(bin);
403 buffer_free(bout);
404 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
Damien Miller95def091999-11-25 00:26:21 +1100406 /* Save old window size. */
407 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller95def091999-11-25 00:26:21 +1100409 /* Send the suspend signal to the program itself. */
410 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Check if the window size has changed. */
413 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
414 (oldws.ws_row != newws.ws_row ||
415 oldws.ws_col != newws.ws_col ||
416 oldws.ws_xpixel != newws.ws_xpixel ||
417 oldws.ws_ypixel != newws.ws_ypixel))
418 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419
Damien Miller95def091999-11-25 00:26:21 +1100420 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000421 buffer_init(bin);
422 buffer_init(bout);
423 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426}
427
Damien Miller4af51302000-04-16 11:18:38 +1000428void
Damien Miller1383bd82000-04-06 12:32:37 +1000429client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430{
Damien Miller1383bd82000-04-06 12:32:37 +1000431 int len;
432 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller5428f641999-11-25 11:54:57 +1100434 /*
435 * Read input from the server, and add any such data to the buffer of
436 * the packet subsystem.
437 */
Damien Miller95def091999-11-25 00:26:21 +1100438 if (FD_ISSET(connection_in, readset)) {
439 /* Read as much as possible. */
440 len = read(connection_in, buf, sizeof(buf));
441 if (len == 0) {
442 /* Received EOF. The remote host has closed the connection. */
443 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
444 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 quit_pending = 1;
447 return;
Damien Miller95def091999-11-25 00:26:21 +1100448 }
Damien Miller5428f641999-11-25 11:54:57 +1100449 /*
450 * There is a kernel bug on Solaris that causes select to
451 * sometimes wake up even though there is no data available.
452 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000453 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100454 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Miller95def091999-11-25 00:26:21 +1100456 if (len < 0) {
457 /* An error has encountered. Perhaps there is a network problem. */
458 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
459 host, strerror(errno));
460 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100461 quit_pending = 1;
462 return;
463 }
464 packet_process_incoming(buf, len);
465 }
Damien Miller1383bd82000-04-06 12:32:37 +1000466}
467
Damien Millerad833b32000-08-23 10:46:23 +1000468/* process the characters one by one */
469int
470process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
471{
472 char string[1024];
473 pid_t pid;
474 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000475 u_int i;
476 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000477 char *s;
478
479 for (i = 0; i < len; i++) {
480 /* Get one character at a time. */
481 ch = buf[i];
482
483 if (escape_pending) {
484 /* We have previously seen an escape character. */
485 /* Clear the flag now. */
486 escape_pending = 0;
487
488 /* Process the escaped character. */
489 switch (ch) {
490 case '.':
491 /* Terminate the connection. */
492 snprintf(string, sizeof string, "%c.\r\n", escape_char);
493 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000494
495 quit_pending = 1;
496 return -1;
497
498 case 'Z' - 64:
499 /* Suspend the program. */
500 /* Print a message to that effect to the user. */
501 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
502 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000503
504 /* Restore terminal modes and suspend. */
505 client_suspend_self(bin, bout, berr);
506
507 /* We have been continued. */
508 continue;
509
Ben Lindstromf28f6342001-04-04 02:03:04 +0000510 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000511 if (compat20) {
512 if (datafellows & SSH_BUG_NOREKEY)
513 log("Server does not support re-keying");
514 else
515 need_rekeying = 1;
516 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000517 continue;
518
Damien Millerad833b32000-08-23 10:46:23 +1000519 case '&':
520 /* XXX does not work yet with proto 2 */
521 if (compat20)
522 continue;
523 /*
524 * Detach the program (continue to serve connections,
525 * but put in background and no more new connections).
526 */
527 if (!stdin_eof) {
528 /*
529 * Sending SSH_CMSG_EOF alone does not always appear
530 * to be enough. So we try to send an EOF character
531 * first.
532 */
533 packet_start(SSH_CMSG_STDIN_DATA);
534 packet_put_string("\004", 1);
535 packet_send();
536 /* Close stdin. */
537 stdin_eof = 1;
538 if (buffer_len(bin) == 0) {
539 packet_start(SSH_CMSG_EOF);
540 packet_send();
541 }
542 }
543 /* Restore tty modes. */
544 leave_raw_mode();
545
546 /* Stop listening for new connections. */
547 channel_stop_listening();
548
549 printf("%c& [backgrounded]\n", escape_char);
550
551 /* Fork into background. */
552 pid = fork();
553 if (pid < 0) {
554 error("fork: %.100s", strerror(errno));
555 continue;
556 }
557 if (pid != 0) { /* This is the parent. */
558 /* The parent just exits. */
559 exit(0);
560 }
561 /* The child continues serving connections. */
562 continue; /*XXX ? */
563
564 case '?':
565 snprintf(string, sizeof string,
566"%c?\r\n\
567Supported escape sequences:\r\n\
568~. - terminate connection\r\n\
569~^Z - suspend ssh\r\n\
570~# - list forwarded connections\r\n\
571~& - background ssh (when waiting for connections to terminate)\r\n\
572~? - this message\r\n\
573~~ - send the escape character by typing it twice\r\n\
574(Note that escapes are only recognized immediately after newline.)\r\n",
575 escape_char);
576 buffer_append(berr, string, strlen(string));
577 continue;
578
579 case '#':
580 snprintf(string, sizeof string, "%c#\r\n", escape_char);
581 buffer_append(berr, string, strlen(string));
582 s = channel_open_message();
583 buffer_append(berr, s, strlen(s));
584 xfree(s);
585 continue;
586
587 default:
588 if (ch != escape_char) {
589 buffer_put_char(bin, escape_char);
590 bytes++;
591 }
592 /* Escaped characters fall through here */
593 break;
594 }
595 } else {
596 /*
597 * The previous character was not an escape char. Check if this
598 * is an escape.
599 */
600 if (last_was_cr && ch == escape_char) {
601 /* It is. Set the flag and continue to next character. */
602 escape_pending = 1;
603 continue;
604 }
605 }
606
607 /*
608 * Normal character. Record whether it was a newline,
609 * and append it to the buffer.
610 */
611 last_was_cr = (ch == '\r' || ch == '\n');
612 buffer_put_char(bin, ch);
613 bytes++;
614 }
615 return bytes;
616}
617
Damien Miller4af51302000-04-16 11:18:38 +1000618void
Damien Miller1383bd82000-04-06 12:32:37 +1000619client_process_input(fd_set * readset)
620{
Damien Miller166fca82000-04-20 07:42:21 +1000621 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000622 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000623
Damien Miller95def091999-11-25 00:26:21 +1100624 /* Read input from stdin. */
625 if (FD_ISSET(fileno(stdin), readset)) {
626 /* Read as much as possible. */
627 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000628 if (len < 0 && (errno == EAGAIN || errno == EINTR))
629 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100630 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100631 /*
632 * Received EOF or error. They are treated
633 * similarly, except that an error message is printed
634 * if it was an error condition.
635 */
Damien Miller95def091999-11-25 00:26:21 +1100636 if (len < 0) {
637 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
638 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100639 }
640 /* Mark that we have seen EOF. */
641 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100642 /*
643 * Send an EOF message to the server unless there is
644 * data in the buffer. If there is data in the
645 * buffer, no message will be sent now. Code
646 * elsewhere will send the EOF when the buffer
647 * becomes empty if stdin_eof is set.
648 */
Damien Miller95def091999-11-25 00:26:21 +1100649 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650 packet_start(SSH_CMSG_EOF);
651 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100652 }
653 } else if (escape_char == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100654 /*
655 * Normal successful read, and no escape character.
656 * Just append the data to buffer.
657 */
Damien Miller95def091999-11-25 00:26:21 +1100658 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100659 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100660 /*
661 * Normal, successful read. But we have an escape character
662 * and have to process the characters one by one.
663 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000664 if (process_escapes(&stdin_buffer, &stdout_buffer,
665 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000666 return;
Damien Miller95def091999-11-25 00:26:21 +1100667 }
668 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669}
670
Damien Miller4af51302000-04-16 11:18:38 +1000671void
Damien Miller95def091999-11-25 00:26:21 +1100672client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673{
Damien Miller95def091999-11-25 00:26:21 +1100674 int len;
675 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Damien Miller95def091999-11-25 00:26:21 +1100677 /* Write buffered output to stdout. */
678 if (FD_ISSET(fileno(stdout), writeset)) {
679 /* Write as much data as possible. */
680 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100681 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100682 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000683 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100684 len = 0;
685 else {
Damien Miller5428f641999-11-25 11:54:57 +1100686 /*
687 * An error or EOF was encountered. Put an
688 * error message to stderr buffer.
689 */
Damien Miller95def091999-11-25 00:26:21 +1100690 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
691 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100692 quit_pending = 1;
693 return;
694 }
695 }
696 /* Consume printed data from the buffer. */
697 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000698 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699 }
Damien Miller95def091999-11-25 00:26:21 +1100700 /* Write buffered output to stderr. */
701 if (FD_ISSET(fileno(stderr), writeset)) {
702 /* Write as much data as possible. */
703 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100704 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100705 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000706 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100707 len = 0;
708 else {
Damien Miller5428f641999-11-25 11:54:57 +1100709 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100710 quit_pending = 1;
711 return;
712 }
713 }
714 /* Consume printed characters from the buffer. */
715 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000716 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100717 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718}
719
Damien Miller5428f641999-11-25 11:54:57 +1100720/*
Damien Millerb38eff82000-04-01 11:09:21 +1000721 * Get packets from the connection input buffer, and process them as long as
722 * there are packets available.
723 *
724 * Any unknown packets received during the actual
725 * session cause the session to terminate. This is
726 * intended to make debugging easier since no
727 * confirmations are sent. Any compatible protocol
728 * extensions must be negotiated during the
729 * preparatory phase.
730 */
731
Damien Miller4af51302000-04-16 11:18:38 +1000732void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000733client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000734{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000735 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000736}
737
Damien Millerad833b32000-08-23 10:46:23 +1000738/* scan buf[] for '~' before sending data to the peer */
739
740int
741simple_escape_filter(Channel *c, char *buf, int len)
742{
743 /* XXX we assume c->extended is writeable */
744 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
745}
746
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000747void
748client_channel_closed(int id, void *arg)
749{
750 if (id != session_ident)
751 error("client_channel_closed: id %d != session_ident %d",
752 id, session_ident);
753 session_closed = 1;
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000754 if (in_raw_mode())
Ben Lindstromd3447632001-04-08 18:07:22 +0000755 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000756}
757
Damien Millerb38eff82000-04-01 11:09:21 +1000758/*
Damien Miller5428f641999-11-25 11:54:57 +1100759 * Implements the interactive session with the server. This is called after
760 * the user has been authenticated, and a command has been started on the
761 * remote host. If escape_char != -1, it is the character used as an escape
762 * character for terminating or suspending the session.
763 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764
Damien Miller4af51302000-04-16 11:18:38 +1000765int
Damien Millerad833b32000-08-23 10:46:23 +1000766client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767{
Damien Miller5e953212001-01-30 09:14:00 +1100768 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100769 double start_time, total_time;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000770 int max_fd = 0, len, rekeying = 0;
Damien Miller95def091999-11-25 00:26:21 +1100771 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772
Damien Miller95def091999-11-25 00:26:21 +1100773 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774
Damien Miller95def091999-11-25 00:26:21 +1100775 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776
Damien Miller95def091999-11-25 00:26:21 +1100777 /* Initialize variables. */
778 escape_pending = 0;
779 last_was_cr = 1;
780 exit_status = -1;
781 stdin_eof = 0;
782 buffer_high = 64 * 1024;
783 connection_in = packet_get_connection_in();
784 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100785 max_fd = MAX(connection_in, connection_out);
786
787 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000788 /* enable nonblocking unless tty */
789 if (!isatty(fileno(stdin)))
790 set_nonblock(fileno(stdin));
791 if (!isatty(fileno(stdout)))
792 set_nonblock(fileno(stdout));
793 if (!isatty(fileno(stderr)))
794 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100795 max_fd = MAX(max_fd, fileno(stdin));
796 max_fd = MAX(max_fd, fileno(stdout));
797 max_fd = MAX(max_fd, fileno(stderr));
798 }
Damien Miller95def091999-11-25 00:26:21 +1100799 stdin_bytes = 0;
800 stdout_bytes = 0;
801 stderr_bytes = 0;
802 quit_pending = 0;
803 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Damien Miller95def091999-11-25 00:26:21 +1100805 /* Initialize buffers. */
806 buffer_init(&stdin_buffer);
807 buffer_init(&stdout_buffer);
808 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809
Damien Millerb38eff82000-04-01 11:09:21 +1000810 client_init_dispatch();
811
Damien Miller95def091999-11-25 00:26:21 +1100812 /* Set signal handlers to restore non-blocking mode. */
813 signal(SIGINT, signal_handler);
814 signal(SIGQUIT, signal_handler);
815 signal(SIGTERM, signal_handler);
816 signal(SIGPIPE, SIG_IGN);
817 if (have_pty)
818 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819
Damien Miller95def091999-11-25 00:26:21 +1100820 if (have_pty)
821 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Ben Lindstrom5b828322001-02-09 01:34:36 +0000823 if (compat20) {
824 session_ident = ssh2_chan_id;
825 if (escape_char != -1)
826 channel_register_filter(session_ident,
827 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000828 if (session_ident != -1)
829 channel_register_cleanup(session_ident,
830 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000831 } else {
832 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000833 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000834 }
Damien Millerad833b32000-08-23 10:46:23 +1000835
Damien Miller95def091999-11-25 00:26:21 +1100836 /* Main loop of the client for the interactive session mode. */
837 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838
Damien Miller5428f641999-11-25 11:54:57 +1100839 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100840 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000842 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000843 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000844
845 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000846
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000847 if (rekeying) {
848 debug("rekeying in progress");
849 } else {
850 /*
851 * Make packets of buffered stdin data, and buffer
852 * them for sending to the server.
853 */
854 if (!compat20)
855 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000857 /*
858 * Make packets from buffered channel data, and
859 * enqueue them for sending to the server.
860 */
861 if (packet_not_very_much_data_to_write())
862 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000864 /*
865 * Check if the window size has changed, and buffer a
866 * message about it to the server if so.
867 */
868 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000870 if (quit_pending)
871 break;
872 }
Damien Miller5428f641999-11-25 11:54:57 +1100873 /*
874 * Wait until we have something to do (something becomes
875 * available on one of the descriptors).
876 */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000877 client_wait_until_can_do_something(&readset, &writeset,
878 &max_fd, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 if (quit_pending)
881 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000883 /* Do channel operations unless rekeying in progress. */
884 if (!rekeying) {
885 channel_after_select(readset, writeset);
886
887 if (need_rekeying) {
888 debug("user requests rekeying");
889 xxx_kex->done = 0;
890 kex_send_kexinit(xxx_kex);
891 need_rekeying = 0;
892 }
893 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
Damien Miller1383bd82000-04-06 12:32:37 +1000895 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +1100896 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller1383bd82000-04-06 12:32:37 +1000898 if (quit_pending)
899 break;
900
901 if (!compat20) {
902 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +1100903 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +1000904 /*
905 * Process output to stdout and stderr. Output to
906 * the connection is processed elsewhere (above).
907 */
Damien Miller5e953212001-01-30 09:14:00 +1100908 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +1000909 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Damien Miller5428f641999-11-25 11:54:57 +1100911 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +1100912 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +1100913 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914 }
Damien Miller5e953212001-01-30 09:14:00 +1100915 if (readset)
916 xfree(readset);
917 if (writeset)
918 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Damien Miller95def091999-11-25 00:26:21 +1100920 /* Terminate the session. */
921
922 /* Stop watching for window change. */
923 if (have_pty)
924 signal(SIGWINCH, SIG_DFL);
925
926 /* Stop listening for connections. */
927 channel_stop_listening();
928
Damien Miller5428f641999-11-25 11:54:57 +1100929 /*
930 * In interactive mode (with pseudo tty) display a message indicating
931 * that the connection has been closed.
932 */
Damien Miller95def091999-11-25 00:26:21 +1100933 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
934 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
935 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 }
Damien Miller95def091999-11-25 00:26:21 +1100937 /* Output any buffered data for stdout. */
938 while (buffer_len(&stdout_buffer) > 0) {
939 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100940 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100941 if (len <= 0) {
942 error("Write failed flushing stdout buffer.");
943 break;
944 }
945 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000946 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100947 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Output any buffered data for stderr. */
950 while (buffer_len(&stderr_buffer) > 0) {
951 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100952 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100953 if (len <= 0) {
954 error("Write failed flushing stderr buffer.");
955 break;
956 }
957 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000958 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100959 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Damien Miller95def091999-11-25 00:26:21 +1100961 if (have_pty)
962 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
Damien Miller95def091999-11-25 00:26:21 +1100964 /* Clear and free any buffers. */
965 memset(buf, 0, sizeof(buf));
966 buffer_free(&stdin_buffer);
967 buffer_free(&stdout_buffer);
968 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
Damien Miller95def091999-11-25 00:26:21 +1100970 /* Report bytes transferred, and transfer rates. */
971 total_time = get_current_time() - start_time;
972 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
973 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
974 if (total_time > 0)
975 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
976 stdin_bytes / total_time, stdout_bytes / total_time,
977 stderr_bytes / total_time);
978
979 /* Return the exit status of the program. */
980 debug("Exit status %d", exit_status);
981 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982}
Damien Millerb38eff82000-04-01 11:09:21 +1000983
984/*********/
985
986void
Damien Miller62cee002000-09-23 17:15:56 +1100987client_input_stdout_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000988{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000989 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000990 char *data = packet_get_string(&data_len);
991 packet_integrity_check(plen, 4 + data_len, type);
992 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000993 memset(data, 0, data_len);
994 xfree(data);
995}
996void
Damien Miller62cee002000-09-23 17:15:56 +1100997client_input_stderr_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000998{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000999 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001000 char *data = packet_get_string(&data_len);
1001 packet_integrity_check(plen, 4 + data_len, type);
1002 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001003 memset(data, 0, data_len);
1004 xfree(data);
1005}
1006void
Damien Miller62cee002000-09-23 17:15:56 +11001007client_input_exit_status(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001008{
1009 packet_integrity_check(plen, 4, type);
1010 exit_status = packet_get_int();
1011 /* Acknowledge the exit. */
1012 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1013 packet_send();
1014 /*
1015 * Must wait for packet to be sent since we are
1016 * exiting the loop.
1017 */
1018 packet_write_wait();
1019 /* Flag that we want to exit. */
1020 quit_pending = 1;
1021}
1022
Damien Miller0bc1bd82000-11-13 22:57:25 +11001023Channel *
1024client_request_forwarded_tcpip(const char *request_type, int rchan)
1025{
1026 Channel* c = NULL;
1027 char *listen_address, *originator_address;
1028 int listen_port, originator_port;
1029 int sock, newch;
1030
1031 /* Get rest of the packet */
1032 listen_address = packet_get_string(NULL);
1033 listen_port = packet_get_int();
1034 originator_address = packet_get_string(NULL);
1035 originator_port = packet_get_int();
1036 packet_done();
1037
1038 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1039 listen_address, listen_port, originator_address, originator_port);
1040
1041 sock = channel_connect_by_listen_adress(listen_port);
1042 if (sock >= 0) {
1043 newch = channel_new("forwarded-tcpip",
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001044 SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001045 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1046 xstrdup(originator_address), 1);
1047 c = channel_lookup(newch);
1048 }
1049 xfree(originator_address);
1050 xfree(listen_address);
1051 return c;
1052}
1053
1054Channel*
1055client_request_x11(const char *request_type, int rchan)
1056{
1057 Channel *c = NULL;
1058 char *originator;
1059 int originator_port;
1060 int sock, newch;
1061
1062 if (!options.forward_x11) {
1063 error("Warning: ssh server tried X11 forwarding.");
1064 error("Warning: this is probably a break in attempt by a malicious server.");
1065 return NULL;
1066 }
1067 originator = packet_get_string(NULL);
1068 if (datafellows & SSH_BUG_X11FWD) {
1069 debug2("buggy server: x11 request w/o originator_port");
1070 originator_port = 0;
1071 } else {
1072 originator_port = packet_get_int();
1073 }
1074 packet_done();
1075 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001076 debug("client_request_x11: request from %s %d", originator,
1077 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001078 sock = x11_connect_display();
1079 if (sock >= 0) {
1080 newch = channel_new("x11",
1081 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1082 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1083 xstrdup("x11"), 1);
1084 c = channel_lookup(newch);
1085 }
1086 xfree(originator);
1087 return c;
1088}
1089
1090Channel*
1091client_request_agent(const char *request_type, int rchan)
1092{
1093 Channel *c = NULL;
1094 int sock, newch;
1095
1096 if (!options.forward_agent) {
1097 error("Warning: ssh server tried agent forwarding.");
1098 error("Warning: this is probably a break in attempt by a malicious server.");
1099 return NULL;
1100 }
1101 sock = ssh_get_authentication_socket();
1102 if (sock >= 0) {
1103 newch = channel_new("authentication agent connection",
1104 SSH_CHANNEL_OPEN, sock, sock, -1,
1105 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1106 xstrdup("authentication agent connection"), 1);
1107 c = channel_lookup(newch);
1108 }
1109 return c;
1110}
1111
Damien Millerbd483e72000-04-30 10:00:53 +10001112/* XXXX move to generic input handler */
1113void
Damien Miller62cee002000-09-23 17:15:56 +11001114client_input_channel_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001115{
1116 Channel *c = NULL;
1117 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001118 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001119 int rchan;
1120 int rmaxpack;
1121 int rwindow;
1122
1123 ctype = packet_get_string(&len);
1124 rchan = packet_get_int();
1125 rwindow = packet_get_int();
1126 rmaxpack = packet_get_int();
1127
Damien Millere247cc42000-05-07 12:03:14 +10001128 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001129 ctype, rchan, rwindow, rmaxpack);
1130
Damien Miller0bc1bd82000-11-13 22:57:25 +11001131 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1132 c = client_request_forwarded_tcpip(ctype, rchan);
1133 } else if (strcmp(ctype, "x11") == 0) {
1134 c = client_request_x11(ctype, rchan);
1135 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1136 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001137 }
1138/* XXX duplicate : */
1139 if (c != NULL) {
1140 debug("confirm %s", ctype);
1141 c->remote_id = rchan;
1142 c->remote_window = rwindow;
1143 c->remote_maxpacket = rmaxpack;
1144
1145 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1146 packet_put_int(c->remote_id);
1147 packet_put_int(c->self);
1148 packet_put_int(c->local_window);
1149 packet_put_int(c->local_maxpacket);
1150 packet_send();
1151 } else {
1152 debug("failure %s", ctype);
1153 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1154 packet_put_int(rchan);
1155 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1156 packet_put_cstring("bla bla");
1157 packet_put_cstring("");
1158 packet_send();
1159 }
1160 xfree(ctype);
1161}
Ben Lindstrom5b828322001-02-09 01:34:36 +00001162void
1163client_input_channel_req(int type, int plen, void *ctxt)
1164{
1165 Channel *c = NULL;
1166 int id, reply, success = 0;
1167 char *rtype;
1168
1169 id = packet_get_int();
1170 rtype = packet_get_string(NULL);
1171 reply = packet_get_char();
1172
1173 debug("client_input_channel_req: channel %d rtype %s reply %d",
1174 id, rtype, reply);
1175
1176 if (session_ident == -1) {
1177 error("client_input_channel_req: no channel %d", session_ident);
1178 } else if (id != session_ident) {
1179 error("client_input_channel_req: channel %d: wrong channel: %d",
1180 session_ident, id);
1181 }
1182 c = channel_lookup(id);
1183 if (c == NULL) {
1184 error("client_input_channel_req: channel %d: unknown channel", id);
1185 } else if (strcmp(rtype, "exit-status") == 0) {
1186 success = 1;
1187 exit_status = packet_get_int();
1188 packet_done();
1189 }
1190 if (reply) {
1191 packet_start(success ?
1192 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1193 packet_put_int(c->remote_id);
1194 packet_send();
1195 }
1196 xfree(rtype);
1197}
Damien Millerbd483e72000-04-30 10:00:53 +10001198
Damien Miller4af51302000-04-16 11:18:38 +10001199void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001200client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001201{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001202 dispatch_init(&dispatch_protocol_error);
Damien Miller1383bd82000-04-06 12:32:37 +10001203 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1204 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1205 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1206 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001207 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001208 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1209 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001210 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001211 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001212
1213 /* rekeying */
1214 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller1383bd82000-04-06 12:32:37 +10001215}
Damien Miller4af51302000-04-16 11:18:38 +10001216void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001217client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001218{
1219 dispatch_init(NULL);
1220 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1221 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1222 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001223 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1224 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1225 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001226 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1227 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1228 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001229
1230 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1231 &auth_input_open_request : &deny_input_open);
1232 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1233 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001234}
Damien Miller4af51302000-04-16 11:18:38 +10001235void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001236client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001237{
1238 client_init_dispatch_13();
1239 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1240 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1241}
Damien Miller4af51302000-04-16 11:18:38 +10001242void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001243client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001244{
Damien Miller1383bd82000-04-06 12:32:37 +10001245 if (compat20)
1246 client_init_dispatch_20();
1247 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001248 client_init_dispatch_13();
1249 else
1250 client_init_dispatch_15();
1251}