blob: 05bcc3add8721c3b8914731e389898ec2ff3bb9d [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 *
14 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
Damien Miller1383bd82000-04-06 12:32:37 +100037 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000038 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110039 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110059 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61#include "includes.h"
Ben Lindstrom44697232001-07-04 03:32:30 +000062RCSID("$OpenBSD: clientloop.c,v 1.78 2001/06/25 08:25:36 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"
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;
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000105static volatile int received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
108static int in_non_blocking_mode = 0;
109
110/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000111static int quit_pending; /* Set to non-zero to quit the client loop. */
112static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100113static int escape_pending; /* Last character was the escape character */
114static int last_was_cr; /* Last character was a newline. */
115static int exit_status; /* Used to store the exit status of the command. */
116static int stdin_eof; /* EOF has been encountered on standard error. */
117static Buffer stdin_buffer; /* Buffer for stdin data. */
118static Buffer stdout_buffer; /* Buffer for stdout data. */
119static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000120static u_long stdin_bytes, stdout_bytes, stderr_bytes;
121static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100122static int connection_in; /* Connection to server (input). */
123static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000124static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000125static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000128int session_ident = -1;
129
Ben Lindstromf28f6342001-04-04 02:03:04 +0000130/*XXX*/
131extern Kex *xxx_kex;
132
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133/* Restores stdin to blocking mode. */
134
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000136leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137{
Damien Miller95def091999-11-25 00:26:21 +1100138 if (in_non_blocking_mode) {
139 (void) fcntl(fileno(stdin), F_SETFL, 0);
140 in_non_blocking_mode = 0;
141 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
142 }
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);
152 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153}
154
Damien Miller5428f641999-11-25 11:54:57 +1100155/*
156 * Signal handler for the window change signal (SIGWINCH). This just sets a
157 * flag indicating that the window has changed.
158 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void
Damien Miller95def091999-11-25 00:26:21 +1100161window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162{
Damien Miller95def091999-11-25 00:26:21 +1100163 received_window_change_signal = 1;
164 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Damien Miller5428f641999-11-25 11:54:57 +1100167/*
168 * Signal handler for signals that cause the program to terminate. These
169 * signals must be trapped to restore terminal modes.
170 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Damien Miller95def091999-11-25 00:26:21 +1100173signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000175 received_signal = sig;
176 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177}
178
Damien Miller5428f641999-11-25 11:54:57 +1100179/*
180 * Returns current time in seconds from Jan 1, 1970 with the maximum
181 * available resolution.
182 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000185get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186{
Damien Miller95def091999-11-25 00:26:21 +1100187 struct timeval tv;
188 gettimeofday(&tv, NULL);
189 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller5428f641999-11-25 11:54:57 +1100192/*
193 * This is called when the interactive is entered. This checks if there is
194 * an EOF coming on stdin. We must check this explicitly, as select() does
195 * not appear to wake up when redirecting from /dev/null.
196 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000199client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200{
Damien Miller95def091999-11-25 00:26:21 +1100201 int len;
202 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller5428f641999-11-25 11:54:57 +1100204 /*
205 * If standard input is to be "redirected from /dev/null", we simply
206 * mark that we have seen an EOF and send an EOF message to the
207 * server. Otherwise, we try to read a single character; it appears
208 * that for some files, such /dev/null, select() never wakes up for
209 * read for this descriptor, which means that we never get EOF. This
210 * way we will get the EOF if stdin comes from /dev/null or similar.
211 */
Damien Miller95def091999-11-25 00:26:21 +1100212 if (stdin_null_flag) {
213 /* Fake EOF on stdin. */
214 debug("Sending eof.");
215 stdin_eof = 1;
216 packet_start(SSH_CMSG_EOF);
217 packet_send();
218 } else {
Damien Miller95def091999-11-25 00:26:21 +1100219 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Damien Miller95def091999-11-25 00:26:21 +1100221 /* Check for immediate EOF on stdin. */
222 len = read(fileno(stdin), buf, 1);
223 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100224 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100225 debug("Sending eof.");
226 stdin_eof = 1;
227 packet_start(SSH_CMSG_EOF);
228 packet_send();
229 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100230 /*
231 * Got data. We must store the data in the buffer,
232 * and also process it as an escape character if
233 * appropriate.
234 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000235 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100236 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000237 else
Damien Miller95def091999-11-25 00:26:21 +1100238 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100239 }
Damien Miller95def091999-11-25 00:26:21 +1100240 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller5428f641999-11-25 11:54:57 +1100245/*
246 * Make packets from buffered stdin data, and buffer them for sending to the
247 * connection.
248 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000251client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000253 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 /* Send buffered stdin data to the server. */
256 while (buffer_len(&stdin_buffer) > 0 &&
257 packet_not_very_much_data_to_write()) {
258 len = buffer_len(&stdin_buffer);
259 /* Keep the packets at reasonable size. */
260 if (len > packet_get_maxsize())
261 len = packet_get_maxsize();
262 packet_start(SSH_CMSG_STDIN_DATA);
263 packet_put_string(buffer_ptr(&stdin_buffer), len);
264 packet_send();
265 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000266 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100267 /* If we have a pending EOF, send it now. */
268 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
269 packet_start(SSH_CMSG_EOF);
270 packet_send();
271 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
Damien Miller5428f641999-11-25 11:54:57 +1100275/*
276 * Checks if the client window has changed, and sends a packet about it to
277 * the server if so. The actual change is detected elsewhere (by a software
278 * interrupt on Unix); this just checks the flag and sends a message if
279 * appropriate.
280 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000283client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284{
Damien Miller1383bd82000-04-06 12:32:37 +1000285 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller1383bd82000-04-06 12:32:37 +1000287 if (! received_window_change_signal)
288 return;
289 /** XXX race */
290 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Damien Miller1383bd82000-04-06 12:32:37 +1000292 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
293 return;
294
Damien Millerd3444942000-09-30 14:20:03 +1100295 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000296
297 if (compat20) {
298 channel_request_start(session_ident, "window-change", 0);
299 packet_put_int(ws.ws_col);
300 packet_put_int(ws.ws_row);
301 packet_put_int(ws.ws_xpixel);
302 packet_put_int(ws.ws_ypixel);
303 packet_send();
304 } else {
305 packet_start(SSH_CMSG_WINDOW_SIZE);
306 packet_put_int(ws.ws_row);
307 packet_put_int(ws.ws_col);
308 packet_put_int(ws.ws_xpixel);
309 packet_put_int(ws.ws_ypixel);
310 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312}
313
Damien Miller5428f641999-11-25 11:54:57 +1100314/*
315 * Waits until the client can do something (some data becomes available on
316 * one of the file descriptors).
317 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Ben Lindstrombba81212001-06-25 05:01:22 +0000319static void
Damien Miller5e953212001-01-30 09:14:00 +1100320client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000321 int *maxfdp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Damien Miller5e953212001-01-30 09:14:00 +1100323 /* Add any selections by the channel mechanism. */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000324 channel_prepare_select(readsetp, writesetp, maxfdp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Damien Miller1383bd82000-04-06 12:32:37 +1000326 if (!compat20) {
327 /* Read from the connection, unless our buffers are full. */
328 if (buffer_len(&stdout_buffer) < buffer_high &&
329 buffer_len(&stderr_buffer) < buffer_high &&
330 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100331 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000332 /*
333 * Read from stdin, unless we have seen EOF or have very much
334 * buffered data to send to the server.
335 */
336 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100337 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000338
339 /* Select stdout/stderr if have data in buffer. */
340 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100341 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000342 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100343 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000344 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000345 /* channel_prepare_select could have closed the last channel */
346 if (session_closed && !channel_still_open()) {
347 if (!packet_have_data_to_write())
348 return;
349 } else {
350 FD_SET(connection_in, *readsetp);
351 }
Damien Miller1383bd82000-04-06 12:32:37 +1000352 }
353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Select server connection if have data to write to the server. */
355 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100356 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100357
Damien Miller5428f641999-11-25 11:54:57 +1100358 /*
359 * Wait for something to happen. This will suspend the process until
360 * some selected descriptor can be read, written, or has some other
361 * event pending. Note: if you want to implement SSH_MSG_IGNORE
362 * messages to fool traffic analysis, this might be the place to do
363 * it: just have a random timeout for the select, and send a random
364 * SSH_MSG_IGNORE packet when the timeout expires.
365 */
Damien Miller95def091999-11-25 00:26:21 +1100366
Damien Miller5e953212001-01-30 09:14:00 +1100367 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100368 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000369
370 /*
371 * We have to clear the select masks, because we return.
372 * We have to return, because the mainloop checks for the flags
373 * set by the signal handlers.
374 */
375 memset(*readsetp, 0, *maxfdp);
376 memset(*writesetp, 0, *maxfdp);
377
Damien Miller95def091999-11-25 00:26:21 +1100378 if (errno == EINTR)
379 return;
380 /* Note: we might still have data in the buffers. */
381 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
382 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100383 quit_pending = 1;
384 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385}
386
Ben Lindstrombba81212001-06-25 05:01:22 +0000387static void
Damien Millerad833b32000-08-23 10:46:23 +1000388client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389{
Damien Miller95def091999-11-25 00:26:21 +1100390 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000393 if (buffer_len(bout) > 0)
394 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
395 if (buffer_len(berr) > 0)
396 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397
Damien Miller95def091999-11-25 00:26:21 +1100398 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Damien Miller5428f641999-11-25 11:54:57 +1100400 /*
401 * Free (and clear) the buffer to reduce the amount of data that gets
402 * written to swap.
403 */
Damien Millerad833b32000-08-23 10:46:23 +1000404 buffer_free(bin);
405 buffer_free(bout);
406 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Damien Miller95def091999-11-25 00:26:21 +1100408 /* Save old window size. */
409 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Damien Miller95def091999-11-25 00:26:21 +1100411 /* Send the suspend signal to the program itself. */
412 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Damien Miller95def091999-11-25 00:26:21 +1100414 /* Check if the window size has changed. */
415 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
416 (oldws.ws_row != newws.ws_row ||
417 oldws.ws_col != newws.ws_col ||
418 oldws.ws_xpixel != newws.ws_xpixel ||
419 oldws.ws_ypixel != newws.ws_ypixel))
420 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Miller95def091999-11-25 00:26:21 +1100422 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000423 buffer_init(bin);
424 buffer_init(bout);
425 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Damien Miller95def091999-11-25 00:26:21 +1100427 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428}
429
Ben Lindstrombba81212001-06-25 05:01:22 +0000430static void
Damien Miller1383bd82000-04-06 12:32:37 +1000431client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432{
Damien Miller1383bd82000-04-06 12:32:37 +1000433 int len;
434 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller5428f641999-11-25 11:54:57 +1100436 /*
437 * Read input from the server, and add any such data to the buffer of
438 * the packet subsystem.
439 */
Damien Miller95def091999-11-25 00:26:21 +1100440 if (FD_ISSET(connection_in, readset)) {
441 /* Read as much as possible. */
442 len = read(connection_in, buf, sizeof(buf));
443 if (len == 0) {
444 /* Received EOF. The remote host has closed the connection. */
445 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
446 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448 quit_pending = 1;
449 return;
Damien Miller95def091999-11-25 00:26:21 +1100450 }
Damien Miller5428f641999-11-25 11:54:57 +1100451 /*
452 * There is a kernel bug on Solaris that causes select to
453 * sometimes wake up even though there is no data available.
454 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000455 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100456 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller95def091999-11-25 00:26:21 +1100458 if (len < 0) {
459 /* An error has encountered. Perhaps there is a network problem. */
460 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
461 host, strerror(errno));
462 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100463 quit_pending = 1;
464 return;
465 }
466 packet_process_incoming(buf, len);
467 }
Damien Miller1383bd82000-04-06 12:32:37 +1000468}
469
Damien Millerad833b32000-08-23 10:46:23 +1000470/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +0000471static int
Damien Millerad833b32000-08-23 10:46:23 +1000472process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
473{
474 char string[1024];
475 pid_t pid;
476 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000477 u_int i;
478 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000479 char *s;
480
481 for (i = 0; i < len; i++) {
482 /* Get one character at a time. */
483 ch = buf[i];
484
485 if (escape_pending) {
486 /* We have previously seen an escape character. */
487 /* Clear the flag now. */
488 escape_pending = 0;
489
490 /* Process the escaped character. */
491 switch (ch) {
492 case '.':
493 /* Terminate the connection. */
494 snprintf(string, sizeof string, "%c.\r\n", escape_char);
495 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000496
497 quit_pending = 1;
498 return -1;
499
500 case 'Z' - 64:
501 /* Suspend the program. */
502 /* Print a message to that effect to the user. */
503 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
504 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000505
506 /* Restore terminal modes and suspend. */
507 client_suspend_self(bin, bout, berr);
508
509 /* We have been continued. */
510 continue;
511
Ben Lindstromf28f6342001-04-04 02:03:04 +0000512 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000513 if (compat20) {
514 if (datafellows & SSH_BUG_NOREKEY)
515 log("Server does not support re-keying");
516 else
517 need_rekeying = 1;
518 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000519 continue;
520
Damien Millerad833b32000-08-23 10:46:23 +1000521 case '&':
522 /* XXX does not work yet with proto 2 */
523 if (compat20)
524 continue;
525 /*
526 * Detach the program (continue to serve connections,
527 * but put in background and no more new connections).
528 */
529 if (!stdin_eof) {
530 /*
531 * Sending SSH_CMSG_EOF alone does not always appear
532 * to be enough. So we try to send an EOF character
533 * first.
534 */
535 packet_start(SSH_CMSG_STDIN_DATA);
536 packet_put_string("\004", 1);
537 packet_send();
538 /* Close stdin. */
539 stdin_eof = 1;
540 if (buffer_len(bin) == 0) {
541 packet_start(SSH_CMSG_EOF);
542 packet_send();
543 }
544 }
545 /* Restore tty modes. */
546 leave_raw_mode();
547
548 /* Stop listening for new connections. */
Ben Lindstrom601e4362001-06-21 03:19:23 +0000549 channel_close_all(); /* proto1 only XXXX */
Damien Millerad833b32000-08-23 10:46:23 +1000550
551 printf("%c& [backgrounded]\n", escape_char);
552
553 /* Fork into background. */
554 pid = fork();
555 if (pid < 0) {
556 error("fork: %.100s", strerror(errno));
557 continue;
558 }
559 if (pid != 0) { /* This is the parent. */
560 /* The parent just exits. */
561 exit(0);
562 }
563 /* The child continues serving connections. */
564 continue; /*XXX ? */
565
566 case '?':
567 snprintf(string, sizeof string,
568"%c?\r\n\
569Supported escape sequences:\r\n\
570~. - terminate connection\r\n\
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000571~R - Request rekey (SSH protocol 2 only)\r\n\
Damien Millerad833b32000-08-23 10:46:23 +1000572~^Z - suspend ssh\r\n\
573~# - list forwarded connections\r\n\
574~& - background ssh (when waiting for connections to terminate)\r\n\
575~? - this message\r\n\
576~~ - send the escape character by typing it twice\r\n\
577(Note that escapes are only recognized immediately after newline.)\r\n",
578 escape_char);
579 buffer_append(berr, string, strlen(string));
580 continue;
581
582 case '#':
583 snprintf(string, sizeof string, "%c#\r\n", escape_char);
584 buffer_append(berr, string, strlen(string));
585 s = channel_open_message();
586 buffer_append(berr, s, strlen(s));
587 xfree(s);
588 continue;
589
590 default:
591 if (ch != escape_char) {
592 buffer_put_char(bin, escape_char);
593 bytes++;
594 }
595 /* Escaped characters fall through here */
596 break;
597 }
598 } else {
599 /*
600 * The previous character was not an escape char. Check if this
601 * is an escape.
602 */
603 if (last_was_cr && ch == escape_char) {
604 /* It is. Set the flag and continue to next character. */
605 escape_pending = 1;
606 continue;
607 }
608 }
609
610 /*
611 * Normal character. Record whether it was a newline,
612 * and append it to the buffer.
613 */
614 last_was_cr = (ch == '\r' || ch == '\n');
615 buffer_put_char(bin, ch);
616 bytes++;
617 }
618 return bytes;
619}
620
Ben Lindstrombba81212001-06-25 05:01:22 +0000621static void
Damien Miller1383bd82000-04-06 12:32:37 +1000622client_process_input(fd_set * readset)
623{
Damien Miller166fca82000-04-20 07:42:21 +1000624 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000625 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000626
Damien Miller95def091999-11-25 00:26:21 +1100627 /* Read input from stdin. */
628 if (FD_ISSET(fileno(stdin), readset)) {
629 /* Read as much as possible. */
630 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000631 if (len < 0 && (errno == EAGAIN || errno == EINTR))
632 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +1100633 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100634 /*
635 * Received EOF or error. They are treated
636 * similarly, except that an error message is printed
637 * if it was an error condition.
638 */
Damien Miller95def091999-11-25 00:26:21 +1100639 if (len < 0) {
640 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
641 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100642 }
643 /* Mark that we have seen EOF. */
644 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100645 /*
646 * Send an EOF message to the server unless there is
647 * data in the buffer. If there is data in the
648 * buffer, no message will be sent now. Code
649 * elsewhere will send the EOF when the buffer
650 * becomes empty if stdin_eof is set.
651 */
Damien Miller95def091999-11-25 00:26:21 +1100652 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653 packet_start(SSH_CMSG_EOF);
654 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100655 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000656 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +1100657 /*
658 * Normal successful read, and no escape character.
659 * Just append the data to buffer.
660 */
Damien Miller95def091999-11-25 00:26:21 +1100661 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100662 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100663 /*
664 * Normal, successful read. But we have an escape character
665 * and have to process the characters one by one.
666 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000667 if (process_escapes(&stdin_buffer, &stdout_buffer,
668 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +1000669 return;
Damien Miller95def091999-11-25 00:26:21 +1100670 }
671 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672}
673
Ben Lindstrombba81212001-06-25 05:01:22 +0000674static void
Damien Miller95def091999-11-25 00:26:21 +1100675client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676{
Damien Miller95def091999-11-25 00:26:21 +1100677 int len;
678 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller95def091999-11-25 00:26:21 +1100680 /* Write buffered output to stdout. */
681 if (FD_ISSET(fileno(stdout), writeset)) {
682 /* Write as much data as possible. */
683 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100684 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100685 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000686 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100687 len = 0;
688 else {
Damien Miller5428f641999-11-25 11:54:57 +1100689 /*
690 * An error or EOF was encountered. Put an
691 * error message to stderr buffer.
692 */
Damien Miller95def091999-11-25 00:26:21 +1100693 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
694 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100695 quit_pending = 1;
696 return;
697 }
698 }
699 /* Consume printed data from the buffer. */
700 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000701 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702 }
Damien Miller95def091999-11-25 00:26:21 +1100703 /* Write buffered output to stderr. */
704 if (FD_ISSET(fileno(stderr), writeset)) {
705 /* Write as much data as possible. */
706 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100707 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100708 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +0000709 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +1100710 len = 0;
711 else {
Damien Miller5428f641999-11-25 11:54:57 +1100712 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100713 quit_pending = 1;
714 return;
715 }
716 }
717 /* Consume printed characters from the buffer. */
718 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000719 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100720 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721}
722
Damien Miller5428f641999-11-25 11:54:57 +1100723/*
Damien Millerb38eff82000-04-01 11:09:21 +1000724 * Get packets from the connection input buffer, and process them as long as
725 * there are packets available.
726 *
727 * Any unknown packets received during the actual
728 * session cause the session to terminate. This is
729 * intended to make debugging easier since no
730 * confirmations are sent. Any compatible protocol
731 * extensions must be negotiated during the
732 * preparatory phase.
733 */
734
Ben Lindstrombba81212001-06-25 05:01:22 +0000735static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000736client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000737{
Ben Lindstromf28f6342001-04-04 02:03:04 +0000738 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000739}
740
Damien Millerad833b32000-08-23 10:46:23 +1000741/* scan buf[] for '~' before sending data to the peer */
742
Ben Lindstrombba81212001-06-25 05:01:22 +0000743static int
Damien Millerad833b32000-08-23 10:46:23 +1000744simple_escape_filter(Channel *c, char *buf, int len)
745{
746 /* XXX we assume c->extended is writeable */
747 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
748}
749
Ben Lindstrombba81212001-06-25 05:01:22 +0000750static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000751client_channel_closed(int id, void *arg)
752{
753 if (id != session_ident)
754 error("client_channel_closed: id %d != session_ident %d",
755 id, session_ident);
756 session_closed = 1;
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000757 if (in_raw_mode())
Ben Lindstromd3447632001-04-08 18:07:22 +0000758 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000759}
760
Damien Millerb38eff82000-04-01 11:09:21 +1000761/*
Damien Miller5428f641999-11-25 11:54:57 +1100762 * Implements the interactive session with the server. This is called after
763 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000764 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
765 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +1100766 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller4af51302000-04-16 11:18:38 +1000768int
Damien Millerad833b32000-08-23 10:46:23 +1000769client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770{
Damien Miller5e953212001-01-30 09:14:00 +1100771 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100772 double start_time, total_time;
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000773 int max_fd = 0, len, rekeying = 0;
Damien Miller95def091999-11-25 00:26:21 +1100774 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775
Damien Miller95def091999-11-25 00:26:21 +1100776 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777
Damien Miller95def091999-11-25 00:26:21 +1100778 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779
Damien Miller95def091999-11-25 00:26:21 +1100780 /* Initialize variables. */
781 escape_pending = 0;
782 last_was_cr = 1;
783 exit_status = -1;
784 stdin_eof = 0;
785 buffer_high = 64 * 1024;
786 connection_in = packet_get_connection_in();
787 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100788 max_fd = MAX(connection_in, connection_out);
789
790 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000791 /* enable nonblocking unless tty */
792 if (!isatty(fileno(stdin)))
793 set_nonblock(fileno(stdin));
794 if (!isatty(fileno(stdout)))
795 set_nonblock(fileno(stdout));
796 if (!isatty(fileno(stderr)))
797 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +1100798 max_fd = MAX(max_fd, fileno(stdin));
799 max_fd = MAX(max_fd, fileno(stdout));
800 max_fd = MAX(max_fd, fileno(stderr));
801 }
Damien Miller95def091999-11-25 00:26:21 +1100802 stdin_bytes = 0;
803 stdout_bytes = 0;
804 stderr_bytes = 0;
805 quit_pending = 0;
806 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807
Damien Miller95def091999-11-25 00:26:21 +1100808 /* Initialize buffers. */
809 buffer_init(&stdin_buffer);
810 buffer_init(&stdout_buffer);
811 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Damien Millerb38eff82000-04-01 11:09:21 +1000813 client_init_dispatch();
814
Damien Miller95def091999-11-25 00:26:21 +1100815 /* Set signal handlers to restore non-blocking mode. */
816 signal(SIGINT, signal_handler);
817 signal(SIGQUIT, signal_handler);
818 signal(SIGTERM, signal_handler);
819 signal(SIGPIPE, SIG_IGN);
820 if (have_pty)
821 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller95def091999-11-25 00:26:21 +1100823 if (have_pty)
824 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825
Ben Lindstrom5b828322001-02-09 01:34:36 +0000826 if (compat20) {
827 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000828 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +0000829 channel_register_filter(session_ident,
830 simple_escape_filter);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000831 if (session_ident != -1)
832 channel_register_cleanup(session_ident,
833 client_channel_closed);
Ben Lindstrom5b828322001-02-09 01:34:36 +0000834 } else {
835 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000836 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +0000837 }
Damien Millerad833b32000-08-23 10:46:23 +1000838
Damien Miller95def091999-11-25 00:26:21 +1100839 /* Main loop of the client for the interactive session mode. */
840 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841
Damien Miller5428f641999-11-25 11:54:57 +1100842 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100843 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000845 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +1000846 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000847
848 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +1000849
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000850 if (rekeying) {
851 debug("rekeying in progress");
852 } else {
853 /*
854 * Make packets of buffered stdin data, and buffer
855 * them for sending to the server.
856 */
857 if (!compat20)
858 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000860 /*
861 * Make packets from buffered channel data, and
862 * enqueue them for sending to the server.
863 */
864 if (packet_not_very_much_data_to_write())
865 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000867 /*
868 * Check if the window size has changed, and buffer a
869 * message about it to the server if so.
870 */
871 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000873 if (quit_pending)
874 break;
875 }
Damien Miller5428f641999-11-25 11:54:57 +1100876 /*
877 * Wait until we have something to do (something becomes
878 * available on one of the descriptors).
879 */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000880 client_wait_until_can_do_something(&readset, &writeset,
881 &max_fd, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
Damien Miller95def091999-11-25 00:26:21 +1100883 if (quit_pending)
884 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000886 /* Do channel operations unless rekeying in progress. */
887 if (!rekeying) {
888 channel_after_select(readset, writeset);
889
890 if (need_rekeying) {
891 debug("user requests rekeying");
892 xxx_kex->done = 0;
893 kex_send_kexinit(xxx_kex);
894 need_rekeying = 0;
895 }
896 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller1383bd82000-04-06 12:32:37 +1000898 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +1100899 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller1383bd82000-04-06 12:32:37 +1000901 if (quit_pending)
902 break;
903
904 if (!compat20) {
905 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +1100906 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +1000907 /*
908 * Process output to stdout and stderr. Output to
909 * the connection is processed elsewhere (above).
910 */
Damien Miller5e953212001-01-30 09:14:00 +1100911 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +1000912 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Damien Miller5428f641999-11-25 11:54:57 +1100914 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +1100915 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +1100916 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 }
Damien Miller5e953212001-01-30 09:14:00 +1100918 if (readset)
919 xfree(readset);
920 if (writeset)
921 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922
Damien Miller95def091999-11-25 00:26:21 +1100923 /* Terminate the session. */
924
925 /* Stop watching for window change. */
926 if (have_pty)
927 signal(SIGWINCH, SIG_DFL);
928
Ben Lindstrom601e4362001-06-21 03:19:23 +0000929 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +1100930
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000931 if (have_pty)
932 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000933
934 /* restore blocking io */
935 if (!isatty(fileno(stdin)))
936 unset_nonblock(fileno(stdin));
937 if (!isatty(fileno(stdout)))
938 unset_nonblock(fileno(stdout));
939 if (!isatty(fileno(stderr)))
940 unset_nonblock(fileno(stderr));
941
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000942 if (received_signal) {
943 if (in_non_blocking_mode) /* XXX */
944 leave_non_blocking();
945 fatal("Killed by signal %d.", received_signal);
946 }
947
948 /*
949 * In interactive mode (with pseudo tty) display a message indicating
950 * that the connection has been closed.
951 */
952 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
953 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
954 buffer_append(&stderr_buffer, buf, strlen(buf));
955 }
956
Damien Miller95def091999-11-25 00:26:21 +1100957 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000958 while (buffer_len(&stdout_buffer) > 0) {
959 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100960 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000961 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100962 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000963 break;
964 }
Damien Miller95def091999-11-25 00:26:21 +1100965 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000966 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100967 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000970 while (buffer_len(&stderr_buffer) > 0) {
971 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100972 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000973 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100974 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000975 break;
976 }
Damien Miller95def091999-11-25 00:26:21 +1100977 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +0000978 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100979 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* Clear and free any buffers. */
982 memset(buf, 0, sizeof(buf));
983 buffer_free(&stdin_buffer);
984 buffer_free(&stdout_buffer);
985 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 /* Report bytes transferred, and transfer rates. */
988 total_time = get_current_time() - start_time;
989 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
990 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
991 if (total_time > 0)
992 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
993 stdin_bytes / total_time, stdout_bytes / total_time,
994 stderr_bytes / total_time);
995
996 /* Return the exit status of the program. */
997 debug("Exit status %d", exit_status);
998 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999}
Damien Millerb38eff82000-04-01 11:09:21 +10001000
1001/*********/
1002
Ben Lindstrombba81212001-06-25 05:01:22 +00001003static void
Damien Miller62cee002000-09-23 17:15:56 +11001004client_input_stdout_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001005{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001006 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001007 char *data = packet_get_string(&data_len);
1008 packet_integrity_check(plen, 4 + data_len, type);
1009 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001010 memset(data, 0, data_len);
1011 xfree(data);
1012}
Ben Lindstrombba81212001-06-25 05:01:22 +00001013static void
Damien Miller62cee002000-09-23 17:15:56 +11001014client_input_stderr_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001015{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001016 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001017 char *data = packet_get_string(&data_len);
1018 packet_integrity_check(plen, 4 + data_len, type);
1019 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001020 memset(data, 0, data_len);
1021 xfree(data);
1022}
Ben Lindstrombba81212001-06-25 05:01:22 +00001023static void
Damien Miller62cee002000-09-23 17:15:56 +11001024client_input_exit_status(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001025{
1026 packet_integrity_check(plen, 4, type);
1027 exit_status = packet_get_int();
1028 /* Acknowledge the exit. */
1029 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1030 packet_send();
1031 /*
1032 * Must wait for packet to be sent since we are
1033 * exiting the loop.
1034 */
1035 packet_write_wait();
1036 /* Flag that we want to exit. */
1037 quit_pending = 1;
1038}
1039
Ben Lindstrombba81212001-06-25 05:01:22 +00001040static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001041client_request_forwarded_tcpip(const char *request_type, int rchan)
1042{
1043 Channel* c = NULL;
1044 char *listen_address, *originator_address;
1045 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001046 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001047
1048 /* Get rest of the packet */
1049 listen_address = packet_get_string(NULL);
1050 listen_port = packet_get_int();
1051 originator_address = packet_get_string(NULL);
1052 originator_port = packet_get_int();
1053 packet_done();
1054
1055 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1056 listen_address, listen_port, originator_address, originator_port);
1057
1058 sock = channel_connect_by_listen_adress(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001059 if (sock < 0) {
1060 xfree(originator_address);
1061 xfree(listen_address);
1062 return NULL;
1063 }
1064 c = channel_new("forwarded-tcpip",
1065 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1066 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1067 xstrdup(originator_address), 1);
1068 if (c == NULL) {
1069 error("client_request_forwarded_tcpip: channel_new failed");
1070 close(sock);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001071 }
1072 xfree(originator_address);
1073 xfree(listen_address);
1074 return c;
1075}
1076
Ben Lindstrombba81212001-06-25 05:01:22 +00001077static Channel*
Damien Miller0bc1bd82000-11-13 22:57:25 +11001078client_request_x11(const char *request_type, int rchan)
1079{
1080 Channel *c = NULL;
1081 char *originator;
1082 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001083 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001084
1085 if (!options.forward_x11) {
1086 error("Warning: ssh server tried X11 forwarding.");
1087 error("Warning: this is probably a break in attempt by a malicious server.");
1088 return NULL;
1089 }
1090 originator = packet_get_string(NULL);
1091 if (datafellows & SSH_BUG_X11FWD) {
1092 debug2("buggy server: x11 request w/o originator_port");
1093 originator_port = 0;
1094 } else {
1095 originator_port = packet_get_int();
1096 }
1097 packet_done();
1098 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001099 debug("client_request_x11: request from %s %d", originator,
1100 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001101 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001102 sock = x11_connect_display();
1103 if (sock < 0)
1104 return NULL;
1105 c = channel_new("x11",
1106 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1107 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1108 xstrdup("x11"), 1);
1109 if (c == NULL) {
1110 error("client_request_x11: channel_new failed");
1111 close(sock);
1112 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001113 return c;
1114}
1115
Ben Lindstrombba81212001-06-25 05:01:22 +00001116static Channel*
Damien Miller0bc1bd82000-11-13 22:57:25 +11001117client_request_agent(const char *request_type, int rchan)
1118{
1119 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001120 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121
1122 if (!options.forward_agent) {
1123 error("Warning: ssh server tried agent forwarding.");
1124 error("Warning: this is probably a break in attempt by a malicious server.");
1125 return NULL;
1126 }
1127 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001128 if (sock < 0)
1129 return NULL;
1130 c = channel_new("authentication agent connection",
1131 SSH_CHANNEL_OPEN, sock, sock, -1,
1132 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1133 xstrdup("authentication agent connection"), 1);
1134 if (c == NULL) {
1135 error("client_request_agent: channel_new failed");
1136 close(sock);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 }
1138 return c;
1139}
1140
Damien Millerbd483e72000-04-30 10:00:53 +10001141/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001142static void
Damien Miller62cee002000-09-23 17:15:56 +11001143client_input_channel_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001144{
1145 Channel *c = NULL;
1146 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001147 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001148 int rchan;
1149 int rmaxpack;
1150 int rwindow;
1151
1152 ctype = packet_get_string(&len);
1153 rchan = packet_get_int();
1154 rwindow = packet_get_int();
1155 rmaxpack = packet_get_int();
1156
Damien Millere247cc42000-05-07 12:03:14 +10001157 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001158 ctype, rchan, rwindow, rmaxpack);
1159
Damien Miller0bc1bd82000-11-13 22:57:25 +11001160 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1161 c = client_request_forwarded_tcpip(ctype, rchan);
1162 } else if (strcmp(ctype, "x11") == 0) {
1163 c = client_request_x11(ctype, rchan);
1164 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1165 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001166 }
1167/* XXX duplicate : */
1168 if (c != NULL) {
1169 debug("confirm %s", ctype);
1170 c->remote_id = rchan;
1171 c->remote_window = rwindow;
1172 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001173 if (c->type != SSH_CHANNEL_CONNECTING) {
1174 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1175 packet_put_int(c->remote_id);
1176 packet_put_int(c->self);
1177 packet_put_int(c->local_window);
1178 packet_put_int(c->local_maxpacket);
1179 packet_send();
1180 }
Damien Millerbd483e72000-04-30 10:00:53 +10001181 } else {
1182 debug("failure %s", ctype);
1183 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1184 packet_put_int(rchan);
1185 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001186 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001187 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001188 packet_put_cstring("");
1189 }
Damien Millerbd483e72000-04-30 10:00:53 +10001190 packet_send();
1191 }
1192 xfree(ctype);
1193}
Ben Lindstrombba81212001-06-25 05:01:22 +00001194static void
Ben Lindstrom5b828322001-02-09 01:34:36 +00001195client_input_channel_req(int type, int plen, void *ctxt)
1196{
1197 Channel *c = NULL;
1198 int id, reply, success = 0;
1199 char *rtype;
1200
1201 id = packet_get_int();
1202 rtype = packet_get_string(NULL);
1203 reply = packet_get_char();
1204
1205 debug("client_input_channel_req: channel %d rtype %s reply %d",
1206 id, rtype, reply);
1207
1208 if (session_ident == -1) {
1209 error("client_input_channel_req: no channel %d", session_ident);
1210 } else if (id != session_ident) {
1211 error("client_input_channel_req: channel %d: wrong channel: %d",
1212 session_ident, id);
1213 }
1214 c = channel_lookup(id);
1215 if (c == NULL) {
1216 error("client_input_channel_req: channel %d: unknown channel", id);
1217 } else if (strcmp(rtype, "exit-status") == 0) {
1218 success = 1;
1219 exit_status = packet_get_int();
1220 packet_done();
1221 }
1222 if (reply) {
1223 packet_start(success ?
1224 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1225 packet_put_int(c->remote_id);
1226 packet_send();
1227 }
1228 xfree(rtype);
1229}
Damien Millerbd483e72000-04-30 10:00:53 +10001230
Ben Lindstrombba81212001-06-25 05:01:22 +00001231static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001232client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001233{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001234 dispatch_init(&dispatch_protocol_error);
Damien Miller1383bd82000-04-06 12:32:37 +10001235 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1236 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1237 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1238 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001239 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001240 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1241 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001242 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001243 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001244
1245 /* rekeying */
1246 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller1383bd82000-04-06 12:32:37 +10001247}
Ben Lindstrombba81212001-06-25 05:01:22 +00001248static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001249client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001250{
1251 dispatch_init(NULL);
1252 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1253 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1254 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001255 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1256 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1257 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001258 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1259 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1260 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001261
1262 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1263 &auth_input_open_request : &deny_input_open);
1264 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1265 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001266}
Ben Lindstrombba81212001-06-25 05:01:22 +00001267static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001268client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001269{
1270 client_init_dispatch_13();
1271 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1272 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1273}
Damien Miller4af51302000-04-16 11:18:38 +10001274void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001275client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001276{
Damien Miller1383bd82000-04-06 12:32:37 +10001277 if (compat20)
1278 client_init_dispatch_20();
1279 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001280 client_init_dispatch_13();
1281 else
1282 client_init_dispatch_15();
1283}